Skip to content

Commit bf6877e

Browse files
mrliklkaiz-ioMichael Kaiser
authored
Added a new example for the usage of CDK pipelines for container builds using source code and multi-region deployments (#1051)
* Added elasticfilesystem:ClientMount to the fargate-service-with-ecs example * Fix: Add grant * added cdk pipeline example * Fix: there is no test so remove package.json script command * Add skip testing --------- Co-authored-by: Michael Kaiser <[email protected]> Co-authored-by: Michael Kaiser <[email protected]>
1 parent 28cb9be commit bf6877e

File tree

15 files changed

+307
-1
lines changed

15 files changed

+307
-1
lines changed

scripts/build-typescript.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ echo "=============================="
1515
cd $scriptdir/../$(dirname $projFile)
1616
if [ -f DO_NOT_AUTOTEST ]; then
1717
echo "found DO_NOT_AUTOTEST, skip it."
18-
return
18+
exit 0
1919
fi
2020
# Check if yarn.lock exists
2121
if [ -f "yarn.lock" ]; then
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.js
2+
!jest.config.js
3+
*.d.ts
4+
node_modules
5+
6+
# CDK asset staging directory
7+
.cdk.staging
8+
cdk.out

typescript/cdkpipeline-ecs/DO_NOT_AUTOTEST

Whitespace-only changes.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# CDK Pipeline for multi-region ECS deployments
2+
3+
## Architecture
4+
5+
![Architecture](./assets/cdk-pipeline-ecs.jpg)
6+
7+
## Overview
8+
9+
[CDK Pipelines](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.pipelines-readme.html) is an opinionated construct library. It is purpose-built to deploy one or more copies of your CDK applications using CloudFormation with a minimal amount of effort on your part.
10+
11+
In this example, CDK pipelines is used to deploy an [ApplicationLoadBalancedFargateService](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ecs_patterns.ApplicationLoadBalancedFargateService.html), along with build of the application container image. The source code containes the source of the pipeline along with the application and related DOCKERFILE.
12+
13+
This example highlights the amount of code required build a fully functional pipeline to get started with rather than the ECS Fargate service, hence the application is a "hello world".
14+
15+
## Pre-requisistes
16+
17+
Create a [Connection](https://docs.aws.amazon.com/dtconsole/latest/userguide/connections.html) to GitHub in your AWS account and provide the arn to the prop `connectionArn` in the pipeline. This connection would be used to pull the source code. Alternatively you can also use code commit. A full list of sources are available [here](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.pipelines.CodePipelineSource.html).
18+
19+
Replace the connection arn, repo name and branch in the [pipelines.ts](./lib/pipeline-stack.ts) file
20+
21+
## Useful CDK commands
22+
* `npm run build` compile typescript to js
23+
* `npm run watch` watch for changes and compile
24+
* `npm run test` perform the jest unit tests
25+
* `npx cdk deploy` deploy this stack to your default AWS account/region
26+
* `npx cdk diff` compare deployed stack with current state
27+
* `npx cdk synth` emits the synthesized CloudFormation template
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Use a Python image as the base image
2+
FROM python:3
3+
4+
# Set the working directory inside the container
5+
WORKDIR /
6+
7+
# Copy the requirements.txt file to the container
8+
COPY requirements.txt .
9+
10+
# Install the required packages
11+
RUN pip3 install --no-cache-dir -r requirements.txt
12+
13+
# Copy the rest of the application code to the container
14+
COPY . .
15+
16+
# Specify the command to run when the container is started
17+
CMD [ "python3", "./app.py" ]
18+
19+
#Expose app port
20+
EXPOSE 80
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from flask import Flask, request
2+
import json
3+
4+
app = Flask(__name__)
5+
6+
7+
@app.route("/", methods=["GET"])
8+
def default_get():
9+
return json.dumps({"message": "Hello from ECS container"})
10+
11+
12+
@app.route("/health", methods=["GET"])
13+
def health_check():
14+
return json.dumps({"message": "Health Check pass"})
15+
16+
17+
if __name__ == "__main__":
18+
app.run(host='0.0.0.0', port=80)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
flask
57.4 KB
Loading
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/usr/bin/env node
2+
import 'source-map-support/register';
3+
import * as cdk from 'aws-cdk-lib';
4+
import { PipelineStack } from '../lib/pipeline-stack';
5+
6+
const app = new cdk.App();
7+
new PipelineStack(app, 'CdkpipeStack', {
8+
// env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },
9+
// env: { account: '123456789012', region: 'us-east-1' },
10+
});
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"app": "npx ts-node --prefer-ts-exts bin/cdkpipe.ts",
3+
"watch": {
4+
"include": [
5+
"**"
6+
],
7+
"exclude": [
8+
"README.md",
9+
"cdk*.json",
10+
"**/*.d.ts",
11+
"**/*.js",
12+
"tsconfig.json",
13+
"package*.json",
14+
"yarn.lock",
15+
"node_modules",
16+
"test"
17+
]
18+
},
19+
"context": {
20+
"@aws-cdk/aws-lambda:recognizeLayerVersion": true,
21+
"@aws-cdk/core:checkSecretUsage": true,
22+
"@aws-cdk/core:target-partitions": [
23+
"aws",
24+
"aws-cn"
25+
],
26+
"@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true,
27+
"@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true,
28+
"@aws-cdk/aws-ecs:arnFormatIncludesClusterName": true,
29+
"@aws-cdk/aws-iam:minimizePolicies": true,
30+
"@aws-cdk/core:validateSnapshotRemovalPolicy": true,
31+
"@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName": true,
32+
"@aws-cdk/aws-s3:createDefaultLoggingPolicy": true,
33+
"@aws-cdk/aws-sns-subscriptions:restrictSqsDescryption": true,
34+
"@aws-cdk/aws-apigateway:disableCloudWatchRole": true,
35+
"@aws-cdk/core:enablePartitionLiterals": true,
36+
"@aws-cdk/aws-events:eventsTargetQueueSameAccount": true,
37+
"@aws-cdk/aws-iam:standardizedServicePrincipals": true,
38+
"@aws-cdk/aws-ecs:disableExplicitDeploymentControllerForCircuitBreaker": true,
39+
"@aws-cdk/aws-iam:importedRoleStackSafeDefaultPolicyName": true,
40+
"@aws-cdk/aws-s3:serverAccessLogsUseBucketPolicy": true,
41+
"@aws-cdk/aws-route53-patters:useCertificate": true,
42+
"@aws-cdk/customresources:installLatestAwsSdkDefault": false,
43+
"@aws-cdk/aws-rds:databaseProxyUniqueResourceName": true,
44+
"@aws-cdk/aws-codedeploy:removeAlarmsFromDeploymentGroup": true,
45+
"@aws-cdk/aws-apigateway:authorizerChangeDeploymentLogicalId": true,
46+
"@aws-cdk/aws-ec2:launchTemplateDefaultUserData": true,
47+
"@aws-cdk/aws-secretsmanager:useAttachedSecretResourcePolicyForSecretTargetAttachments": true,
48+
"@aws-cdk/aws-redshift:columnId": true,
49+
"@aws-cdk/aws-stepfunctions-tasks:enableEmrServicePolicyV2": true,
50+
"@aws-cdk/aws-ec2:restrictDefaultSecurityGroup": true,
51+
"@aws-cdk/aws-apigateway:requestValidatorUniqueId": true,
52+
"@aws-cdk/aws-kms:aliasNameRef": true,
53+
"@aws-cdk/aws-autoscaling:generateLaunchTemplateInsteadOfLaunchConfig": true,
54+
"@aws-cdk/core:includePrefixInUniqueNameGeneration": true,
55+
"@aws-cdk/aws-efs:denyAnonymousAccess": true,
56+
"@aws-cdk/aws-opensearchservice:enableOpensearchMultiAzWithStandby": true,
57+
"@aws-cdk/aws-lambda-nodejs:useLatestRuntimeVersion": true,
58+
"@aws-cdk/aws-efs:mountTargetOrderInsensitiveLogicalId": true,
59+
"@aws-cdk/aws-rds:auroraClusterChangeScopeOfInstanceParameterGroupWithEachParameters": true,
60+
"@aws-cdk/aws-appsync:useArnForSourceApiAssociationIdentifier": true,
61+
"@aws-cdk/aws-rds:preventRenderingDeprecatedCredentials": true,
62+
"@aws-cdk/aws-codepipeline-actions:useNewDefaultBranchForCodeCommitSource": true,
63+
"@aws-cdk/aws-cloudwatch-actions:changeLambdaPermissionLogicalIdForLambdaAction": true,
64+
"@aws-cdk/aws-codepipeline:crossAccountKeysDefaultValueToFalse": true,
65+
"@aws-cdk/aws-codepipeline:defaultPipelineTypeToV2": true,
66+
"@aws-cdk/aws-kms:reduceCrossAccountRegionPolicyScope": true,
67+
"@aws-cdk/aws-eks:nodegroupNameAttribute": true,
68+
"@aws-cdk/aws-ec2:ebsDefaultGp3Volume": true,
69+
"@aws-cdk/aws-ecs:removeDefaultDeploymentAlarm": true
70+
}
71+
}

0 commit comments

Comments
 (0)