@@ -28,7 +28,7 @@ Generates the CloudFormation templates for a "dev stage" build pipeline.
2828Generates the CloudFormation templates for a "production stage" build pipeline.
2929
3030` npm run cdk:synth -- --context STAGE={STAGENAME} `
31- Generates the CloudFormation templates for a build pipeline, stage given by ` {STAGENAME} ` .
31+ Generates the CloudFormation templates for a build pipeline, for the given stage name
3232
3333` npm run cdk:deploy:all `
3434Deploys the synthesized pipeline to an AWS Environment, as defined by your active AWS config profile.
5454
5555` npm run cdk:test:deploy ` - deploys these stacks to AWS as "dev" stage
5656
57- All being successful, you should see the application login screen at ` https://dev.spylogic.ai ` . Log into the AWS Console to add a
58- user to the dev Cognito userpool, then log into the UI to test app deployment was successful.
57+ All being successful, you should see the application login screen at ` https://dev.spylogic.ai ` . Log into the AWS Console
58+ to add a user to the dev Cognito userpool, then log into the UI to test app deployment was successful.
5959
6060` npm run cdk:test:destroy ` - Remember to destroy the stacks after testing, else you will rack up costs!
6161
@@ -66,52 +66,115 @@ user to the dev Cognito userpool, then log into the UI to test app deployment wa
6666At the time of writing, current infrastructure costs us around $60 per month, with just two AZs for the load balancer,
6767deployed into ` eu-north-1 ` . This is one of the [ greenest AWS regions] ( https://app.electricitymaps.com/map ) , but costs
6868are about average. The vast majority of the bill is for the VPC, Load Balancer and NAT EC2 Instance. We have tasks on
69- our todo list to reduce these costs (such as removing the NAT Instance in favour of IPv6 egress), but those are
70- work-in-progress.
69+ our todo list to reduce these costs (radical idea: convert container app to REDIS-backed lambdas).
7170
7271The bottom line: remember to destroy your stacks when no longer needed!
7372
7473## First-time admin tasks
7574
7675When setting up the CDK project for the first time, there are a few one-time tasks you must complete.
7776
78- ### Bootstrapping the CDK using a Developer Policy
77+ ### Bootstrapping the CDK
7978
8079In order to deploy AWS resources to a remote environment using CDK, you must first
8180[ bootstrap the CDK] ( https://docs.aws.amazon.com/cdk/v2/guide/bootstrapping.html ) . For this project, as per
8281[ CDK guidelines] ( https://aws.amazon.com/blogs/devops/secure-cdk-deployments-with-iam-permission-boundaries/ ) , we are
8382using a lightweight permissions boundary to restrict permissions, to prevent creation of new users or roles with
8483elevated permissions. See ` cdk-developer-policy.yaml ` for details.
8584
86- Note that once the pipeline is deployed, it is a good idea to restrict permissions further, so that only the pipeline
87- can make changes to the stacks.
85+ We also have a set of [ IAM Managed Policies ] ( ./ permissions/README.md ) that restrict what CloudFormation is allowed to
86+ do, as CDK by default allows full AdministratorAccess! 😵 🤢 🤮
8887
89- Create the permissions boundary CloudFormation stack:
88+ Note that once the pipeline is deployed, it's a good idea to restrict permissions for developers further, so that only
89+ the pipeline can make changes to the stacks, via approved GitHub merges.
9090
91- ```
91+ 1 . Create permissions boundary stack
92+
93+ ``` shell
9294aws cloudformation create-stack \
9395 --stack-name CDKDeveloperPolicy \
9496 --template-body file://cdk-developer-policy.yaml \
9597 --capabilities CAPABILITY_NAMED_IAM
9698```
9799
98- Then bootstrap the CDK:
99-
100+ 2 . Create IAM managed policies
101+
102+ ``` shell
103+ aws iam create-policy \
104+ --policy-name cdk-execution-policy-basics \
105+ --policy-document file://permissions/execution_policy_basics.json \
106+ --description " Baseline permissions for cloudformation deployments"
107+
108+ aws iam create-policy \
109+ --policy-name cdk-execution-policy-cloudfront \
110+ --policy-document file://permissions/execution_policy_cloudfront.json \
111+ --description " Permissions to deploy cloudfront resources, except for lambda@edge functions"
112+
113+ aws iam create-policy \
114+ --policy-name cdk-execution-policy-cognito \
115+ --policy-document file://permissions/execution_policy_cognito.json \
116+ --description " Permissions to deploy cognito userpools and related resources"
117+
118+ aws iam create-policy \
119+ --policy-name cdk-execution-policy-edgelambda \
120+ --policy-document file://permissions/execution_policy_edgelambda.json \
121+ --description " Permissions to deploy lambda@edge functions for a cloudfront distribution"
122+
123+ aws iam create-policy \
124+ --policy-name cdk-execution-policy-pipeline \
125+ --policy-document file://permissions/execution_policy_pipeline.json \
126+ --description " Permissions to deploy a codepipeline and codebuild projects"
127+
128+ aws iam create-policy \
129+ --policy-name cdk-execution-policy-route53 \
130+ --policy-document file://permissions/execution_policy_route53.json \
131+ --description " Permissions to deploy domain records and ACM certificates"
132+
133+ aws iam create-policy \
134+ --policy-name cdk-execution-policy-vpc \
135+ --policy-document file://permissions/execution_policy_vpc.json \
136+ --description " Permissions to deploy VPC, EC2 and ECS resources for a Fargate-managed container app"
100137```
101- # install dependencies if not already done
102- npm install
103138
104- # run the bootstrap command
105- npx cdk bootstrap --custom-permissions-boundary cdk-developer-policy
139+ 3 . Bootstrap the CDK environment
140+
141+ ``` shell
142+ # install dependencies if you've not already done so
143+ npm install
106144```
107145
108- Unless your default region is ` us-east-1 ` , you will also need to bootstrap this region, as certificates for CloudFront
109- currently need to be deployed there :
146+ If your primary region is NOT ` us-east-1 ` , you will need to bootstrap that region as well, as
147+ currently Lambda@Edge functions can only be deployed to ` us-east-1 ` :
110148
149+ ``` shell
150+ # Bootstrap primary region
151+ npx cdk bootstrap aws://{account}/{region} \
152+ --custom-permissions-boundary cdk-developer-policy \
153+ --cloudformation-execution-policies " arn:aws:iam::{account}:policy/cdk-execution-policy-basics,arn:aws:iam::{account}:policy/cdk-execution-policy-cloudfront,arn:aws:iam::{account}:policy/cdk-execution-policy-cognito,arn:aws:iam::{account}:policy/cdk-execution-policy-pipeline,arn:aws:iam::{account}:policy/cdk-execution-policy-route53,arn:aws:iam::{account}:policy/cdk-execution-policy-vpc"
154+
155+ # Bootstrap us-east-1 for cloudfront
156+ npx cdk bootstrap aws://{account}/us-east-1 \
157+ --custom-permissions-boundary cdk-developer-policy \
158+ --cloudformation-execution-policies " arn:aws:iam::{account}:policy/cdk-execution-policy-basics,arn:aws:iam::{account}:policy/cdk-execution-policy-edgelambda"
111159```
112- npx cdk bootstrap --custom-permissions-boundary cdk-developer-policy aws://YOUR_ACCOUNT_NUMBER/us-east-1
160+
161+ If your primary region IS ` us-east-1 ` , then you only need one bootstrap command:
162+
163+ ``` shell
164+ # Bootstrap us-east-1
165+ npx cdk bootstrap aws://{account}/us-east-1 \
166+ --custom-permissions-boundary cdk-developer-policy \
167+ --cloudformation-execution-policies " arn:aws:iam::{account}:policy/cdk-execution-policy-basics,arn:aws:iam::{account}:policy/cdk-execution-policy-cloudfront,arn:aws:iam::{account}:policy/cdk-execution-policy-cognito,arn:aws:iam::{account}:policy/cdk-execution-policy-edgelambda,arn:aws:iam::{account}:policy/cdk-execution-policy-pipeline,arn:aws:iam::{account}:policy/cdk-execution-policy-route53,arn:aws:iam::{account}:policy/cdk-execution-policy-vpc"
113168```
114169
170+ ### SSM Parameters
171+
172+ There are two Parameters needed when the stacks are deployed, so ensure these are added before you deploy the
173+ pipeline first time:
174+
175+ - ` DOMAIN_NAME ` - Domain where the application will be available
176+ - ` HOSTED_ZONE_ID ` - We advise you create your Hosted Zone manually (via the AWS Console) before deploying the stacks
177+
115178### Server secrets
116179
117180The Node Express server needs a couple of secret values, which are injected into the container environment during
0 commit comments