Skip to content

Commit 302bc99

Browse files
committed
Trigger CodeBuild via Lambda instead of CodePipeline. Update packages. Update readmes. Add test event.
1 parent ca32aae commit 302bc99

23 files changed

+783
-866
lines changed

README.md

+20-4
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ It's easy to scope a song - just send a message starting with 'scope' followed b
88

99
Hit up Scope at [https://m.me/scopebot](https://m.me/scopebot) or [https://facebook.com/scopebot](https://facebook.com/scopebot)
1010

11-
## Technologies Used
11+
### Technologies Used
1212

1313
- Node.js
1414
- TypeScript
1515
- Terraform
1616
- Lex
1717
- Lambda
18-
- CodeBuild/CodePipeline
18+
- CodeBuild
1919
- Facebook Messenger Platform
2020
- Spotify API
2121
- Genius API
2222

23-
## What's next for Scope?
23+
### What's next for Scope?
2424

2525
- Add/improve sharing functionality
2626
- Add suggestions
@@ -29,6 +29,22 @@ Hit up Scope at [https://m.me/scopebot](https://m.me/scopebot) or [https://faceb
2929

3030
> _Do you fools listen to music or do you just skim through it? - JAY-Z_
3131
32-
## Deployment/Infrastructure
32+
---
33+
34+
### Environment variables
35+
36+
- SPOTIFY_CLIENT_ID
37+
- SPOTIFY_CLIENT_SECRET
38+
- GENIUS_ACCESS_TOKEN
39+
- PAGE_ACCESS_TOKEN
40+
41+
### Running locally (with hot reloading)
42+
43+
1. Set environment variables listed above
44+
1. Configure event in [/src/test.ts](./src/test.ts) (see [/src/LexEvent.ts](./src/LexEvent.ts) for event structure)
45+
1. `yarn run test`
46+
47+
48+
### Deployment/Infrastructure
3349

3450
Refer to the [/infrastructure](./infrastructure) directory.

buildspec.yml

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
1+
# All commands below are run from root directory of repository by CodeBuild
12
version: 0.2
23

34
env:
45
variables:
5-
TF_VAR_remote_state_bucket: "603-terraform-remote-state"
6-
TF_VAR_remote_state_region: "ap-southeast-2"
76
TF_VAR_region: "us-east-1"
87
TF_VAR_name: "scope-lex-handler"
8+
TF_VAR_kms_key_arns: '["arn:aws:kms:us-east-1:982898479788:key/3dc41364-c3e6-4544-8e39-c5f4780d19cd","arn:aws:kms:us-east-1:982898479788:key/e83f1d78-5dc5-4aca-a00b-2fc2ac7bebe4"]'
9+
TF_VAR_ssm_parameter_arns: '["arn:aws:ssm:us-east-1:982898479788:parameter/scope-lex-handler/*","arn:aws:ssm:us-east-1:982898479788:parameter/shared/*"]'
910
TF_VAR_build_docker_image: "jch254/docker-node-terraform-aws"
1011
TF_VAR_build_docker_tag: "6.10.0"
11-
TF_VAR_github_repository_owner: "jch254"
12-
TF_VAR_github_repository_name: "scope-lex-handler"
13-
TF_VAR_github_branch_name: "master"
14-
TF_VAR_artifacts_dir: "../dist" # executed from /infrastruture directory
12+
TF_VAR_buildspec: "buildspec.yml"
13+
TF_VAR_source_location: "https://github.com/jch254/scope-lex-handler.git"
14+
TF_VAR_artifacts_dir: "../dist"
1515
TF_VAR_runtime: "nodejs6.10"
1616
TF_VAR_handler: "index.handler"
17-
TF_VAR_kms_key_arns: '["arn:aws:kms:us-east-1:982898479788:key/3dc41364-c3e6-4544-8e39-c5f4780d19cd","arn:aws:kms:us-east-1:982898479788:key/e83f1d78-5dc5-4aca-a00b-2fc2ac7bebe4"]'
18-
TF_VAR_ssm_parameter_arns: '["arn:aws:ssm:us-east-1:982898479788:parameter/scope-lex-handler/*","arn:aws:ssm:us-east-1:982898479788:parameter/shared/*"]'
17+
REMOTE_STATE_BUCKET: "603-terraform-remote-state"
18+
REMOTE_STATE_REGION: "ap-southeast-2"
19+
parameter-store:
20+
SPOTIFY_CLIENT_ID: "/scope-lex-handler/spotify-client-id"
21+
SPOTIFY_CLIENT_SECRET: "/scope-lex-handler/spotify-client-secret"
22+
GENIUS_ACCESS_TOKEN: "/scope-lex-handler/genius-access-token"
23+
PAGE_ACCESS_TOKEN: "/scope-lex-handler/page-access-token"
24+
TF_VAR_github_oauth_token: "/shared/github-token"
1925

2026
phases:
2127
install:
@@ -32,14 +38,8 @@ phases:
3238
- export AWS_SECRET_ACCESS_KEY=`curl --silent http://169.254.170.2:80$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI | jq -r '.SecretAccessKey'`
3339
- export AWS_SESSION_TOKEN=`curl --silent http://169.254.170.2:80$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI | jq -r '.Token'`
3440

35-
- SPOTIFY_CLIENT_ID=$(aws ssm get-parameters --region $TF_VAR_region --name "/scope-lex-handler/spotify-client-id" --with-decryption --query Parameters[0].Value)
36-
- SPOTIFY_CLIENT_SECRET=$(aws ssm get-parameters --region $TF_VAR_region --name "/scope-lex-handler/spotify-client-secret" --with-decryption --query Parameters[0].Value)
37-
- GENIUS_ACCESS_TOKEN=$(aws ssm get-parameters --region $TF_VAR_region --name "/scope-lex-handler/genius-access-token" --with-decryption --query Parameters[0].Value)
38-
- PAGE_ACCESS_TOKEN=$(aws ssm get-parameters --region $TF_VAR_region --name "/scope-lex-handler/page-access-token" --with-decryption --query Parameters[0].Value)
3941
- export TF_VAR_environment_variables="{ SPOTIFY_CLIENT_ID = $SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET = $SPOTIFY_CLIENT_SECRET, GENIUS_ACCESS_TOKEN = $GENIUS_ACCESS_TOKEN, PAGE_ACCESS_TOKEN = $PAGE_ACCESS_TOKEN }"
4042

41-
- export TF_VAR_github_oauth_token=$(aws ssm get-parameters --region $TF_VAR_region --name "/shared/github-token" --with-decryption --query Parameters[0].Value --output text)
42-
4343
build:
4444
commands:
4545
- ./infrastructure/build-artifacts.bash

infrastructure/README.md

+25-20
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# Deployment/Infrastructure
22

3-
Scope-lex-handler is a Lambda function built, tested and deployed to AWS by CodePipeline and CodeBuild.
3+
Scope-lex-handler is a Lambda function built, tested and deployed to AWS by [codebuild-github-webhook](https://github.com/jch254/codebuild-github-webhook) and CodeBuild.
44

55
---
66

7-
**All commands below must be run in each /infrastructure subdirectory (e.g. /infrastructure/admin-api) then in the /infrastructure directory**
7+
### Deployment Prerequisites
8+
9+
**All commands below must be run in the /infrastructure directory.**
810

911
To deploy to AWS, you must:
1012

@@ -13,46 +15,49 @@ To deploy to AWS, you must:
1315
1. Set your credentials as the environment variables `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`.
1416
1. Run `aws configure` and fill in the details it asks for.
1517
1. Run on an EC2 instance with an IAM Role.
16-
1. Run via CodeBuild or ECS Task with an IAM Role (see [buildspec.yml](../buildspec.yml) for workaround)
18+
1. Run via CodeBuild or ECS Task with an IAM Role (see [buildspec-test.yml](../buildspec-test.yml) for workaround)
1719

18-
### Deploying infrastructure
20+
#### Deploying infrastructure
1921

20-
1. Export all environment variables beginning with TF_VAR_ as per [buildspec.yml](../buildspec.yml)
22+
1. Update and export all environment variables specified in the appropriate buildspec declaration (check all phases) and bash scripts
2123
1. Initialise Terraform:
2224
```
2325
terraform init \
2426
-backend-config 'bucket=YOUR_S3_BUCKET' \
2527
-backend-config 'key=YOUR_S3_KEY' \
26-
-backend-config 'region=YOUR_REGION'
28+
-backend-config 'region=YOUR_REGION' \
29+
-get=true \
30+
-upgrade=true
2731
```
28-
1. `terraform get --update`
29-
1. `terraform plan`
30-
1. `terraform apply`
32+
1. `terraform plan -out main.tfplan`
33+
1. `terraform apply main.tfplan`
3134

32-
### Updating infrastructure
35+
#### Updating infrastructure
3336

34-
1. Export all environment variables beginning with TF_VAR_ as per [buildspec.yml](../buildspec.yml)
37+
1. Update and export all environment variables specified in the appropriate buildspec declaration (check all phases) and bash scripts
3538
1. Make necessary infrastructure code changes.
3639
1. Initialise Terraform:
3740
```
3841
terraform init \
3942
-backend-config 'bucket=YOUR_S3_BUCKET' \
4043
-backend-config 'key=YOUR_S3_KEY' \
41-
-backend-config 'region=YOUR_REGION'
44+
-backend-config 'region=YOUR_REGION' \
45+
-get=true \
46+
-upgrade=true
4247
```
43-
1. `terraform get --update`
44-
1. `terraform plan`
45-
1. `terraform apply`
48+
1. `terraform plan -out main.tfplan`
49+
1. `terraform apply main.tfplan`
4650

47-
### Destroying infrastructure (use with care)
51+
#### Destroying infrastructure (use with care)
4852

49-
1. Export all environment variables beginning with TF_VAR_ as per [buildspec.yml](../buildspec.yml)
53+
1. Update and export all environment variables specified in the appropriate buildspec declaration (check all phases) and bash scripts
5054
1. Initialise Terraform:
5155
```
5256
terraform init \
5357
-backend-config 'bucket=YOUR_S3_BUCKET' \
5458
-backend-config 'key=YOUR_S3_KEY' \
55-
-backend-config 'region=YOUR_REGION'
59+
-backend-config 'region=YOUR_REGION' \
60+
-get=true \
61+
-upgrade=true
5662
```
57-
1. `terraform get --update`
58-
1. `terraform destroy`
63+
1. `terraform destroy`

infrastructure/build-artifacts.bash

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
echo Building artifacts...
44

5-
export NODE_ENV=production
65
yarn run build
76
yarn install --production --modules-folder dist/node_modules
87

infrastructure/deploy-infrastructure.bash

+6-5
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ echo Deploying infrastructure via Terraform...
44

55
cd infrastructure
66
terraform init \
7-
-backend-config "bucket=${TF_VAR_remote_state_bucket}" \
7+
-backend-config "bucket=${REMOTE_STATE_BUCKET}" \
88
-backend-config "key=${TF_VAR_name}" \
9-
-backend-config "region=${TF_VAR_remote_state_region}"
10-
terraform get --update
11-
terraform plan
12-
terraform apply
9+
-backend-config "region=${REMOTE_STATE_REGION}" \
10+
-get=true \
11+
-upgrade=true
12+
terraform plan -out main.tfplan
13+
terraform apply main.tfplan
1314
cd ..
1415

1516
echo Finished deploying infrastructure

infrastructure/main.tf

+43-10
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,62 @@
11
terraform {
22
backend "s3" {
3-
encrypt = "true"
3+
encrypt= "true"
44
}
55
}
66

77
provider "aws" {
88
region = "${var.region}"
9+
version = "~> 0.1"
910
}
1011

11-
module "build_pipeline" {
12-
source = "./modules/build-pipeline"
12+
resource "aws_iam_role" "codebuild_role" {
13+
name = "${var.name}-codebuild"
14+
15+
assume_role_policy = <<EOF
16+
{
17+
"Version": "2012-10-17",
18+
"Statement": [
19+
{
20+
"Effect": "Allow",
21+
"Principal": {
22+
"Service": "codebuild.amazonaws.com"
23+
},
24+
"Action": "sts:AssumeRole"
25+
}
26+
]
27+
}
28+
EOF
29+
}
30+
31+
data "template_file" "codebuild_policy" {
32+
template = "${file("./codebuild-role-policy.tpl")}"
33+
34+
vars {
35+
kms_key_arns = "${var.kms_key_arns}"
36+
ssm_parameter_arns = "${var.ssm_parameter_arns}"
37+
}
38+
}
39+
40+
resource "aws_iam_role_policy" "codebuild_policy" {
41+
name = "${var.name}-codebuild-policy"
42+
role = "${aws_iam_role.codebuild_role.id}"
43+
policy = "${data.template_file.codebuild_policy.rendered}"
44+
}
45+
46+
module "codebuild_project" {
47+
source = "github.com/jch254/terraform-modules//codebuild-project?ref=1.0.1"
1348

1449
name = "${var.name}"
15-
kms_key_arns = "${var.kms_key_arns}"
16-
ssm_parameter_arns = "${var.ssm_parameter_arns}"
50+
codebuild_role_arn = "${aws_iam_role.codebuild_role.arn}"
1751
build_docker_image = "${var.build_docker_image}"
1852
build_docker_tag = "${var.build_docker_tag}"
19-
github_oauth_token = "${var.github_oauth_token}"
20-
github_repository_owner = "${var.github_repository_owner}"
21-
github_repository_name = "${var.github_repository_name}"
22-
github_branch_name = "${var.github_branch_name}"
53+
source_type = "${var.source_type}"
54+
buildspec = "${var.buildspec}"
55+
source_location = "${var.source_location}"
2356
}
2457

2558
module "lambda_function" {
26-
source = "./modules/lambda-function"
59+
source = "github.com/jch254/terraform-modules//lambda-function?ref=1.0.1"
2760

2861
name = "${var.name}"
2962
artifacts_dir = "${var.artifacts_dir}"

infrastructure/modules/build-pipeline/codepipeline-role-policy.tpl

-15
This file was deleted.

0 commit comments

Comments
 (0)