This is a quick start project for TypeScript development with CDK. More concept of AWS CDK you can refer: aws-cdk-guide and cdkworkshop
CDKToolkit CloudFormation Stack will be created and make sure it executed successfully.
The cdk.json
file tells the CDK Toolkit how to execute your app.
cdk bootstrap
cdk --app "npx hello-cdk test/hello-cdk.test.ts" ls
mkdir hello-sdk
cdk init --language typescript
cdk ls
The architecture is quick start try to build
# check the compile error
npm run watch
# Compiling the App
npm run build
# Synthesizing an AWS CloudFormation Template
cdk synth
# deploy the project resources
cdk deploy --profile cn-north-1
# destroy the project resources
cdk destroy --profile cn-north-1
# multi stack
cdk deploy CdkWorkshopStack
npm install @aws-cdk/aws-s3
import s3 = require('@aws-cdk/aws-s3');
new s3.Bucket(this, 'BUCKET_NAME', {
versioned: true
});
npm install @aws-cdk/aws-sqs
import sqs = require('@aws-cdk/aws-sqs');
new sqs.Queue(this, "QUEUE_NAME", queueProps);
npm install @aws-cdk/aws-lambda
npm install @aws-cdk/aws-lambda-event-sources
import lambda = require('@aws-cdk/aws-lambda');
import event_sources = require('@aws-cdk/aws-lambda-event-sources');
new lambda.Function(this, "HelloCDK-Function", {
runtime: lambda.Runtime.NODEJS_10_X,
code: lambda.Code.asset('lambda'),
handler: 'index.handler'
});
npm install @aws-cdk/aws-dynamodb
import dynamodb = require('@aws-cdk/aws-dynamodb');
new dynamodb.Table(this, 'HelloCDK-DDB-Table', {
partitionKey: {name: 'id', type: dynamodb.AttributeType.STRING}
});
npm install @aws-cdk/aws-apigateway
import apigw = require('@aws-cdk/aws-apigateway');
new apigw.LambdaRestApi(this, 'CdkWorkshopAPI', {
handler: HelloCounter.lambda_handler,
endpointTypes: [apigw.EndpointType.REGIONAL]
});
Demo aws-ecs-patterns to quick deploy ECS fargate cluster to consume SQS message by using Queue Processing Services
More details please refer aws-ecs-patterns
# Build your SQS Consumer
aws ecr create-repository --repository-name receive-sqs-message --region YOUR_REGION --profile YOUR_PROFILE
docker build -t <repositoryUrl> .
docker run -e AWS_ACCESS_KEY_ID=abc -e AWS_SECRET_ACCESS_KEY=xyz \
<repositoryUrl>:latest ARG1 ARG2
docker push <repositoryUrl>
npm install @aws-cdk/aws-ecs-patterns
import ecs_patterns = require('@aws-cdk/aws-ecs-patterns');
Outputs:
FargateSQSCdkStack.FargateSQSCdkAPIEndpoint4FA94840 = https://j70sgjwic8.execute-api.cn-north-1.amazonaws.com.cn/prod/
FargateSQSCdkStack.MyQueueProcessingServiceSQSQueueCB188552 = FargateSQSCdkStack-FargateSQSCdkQueueDD868C53-C0NCCHF2LSO7
FargateSQSCdkStack.MyQueueProcessingServiceSQSQueueArnC96507D6 = arn:aws-cn:sqs:cn-north-1:876820548815:FargateSQSCdkStack-FargateSQSCdkQueueDD868C53-C0NCCHF2LSO7
Fix: Try to upgrade everything to latest version. aws/aws-cdk#542
Error: Status reason CannotPullECRContainerError: AccessDeniedException: User: arn:aws-cn:sts::876820548815:assumed-role/FargateSQSCdkStack-MyQueueProcessingServiceQueuePr-XVNZR7I99PT3/036623ba-fb6e-4df9-8900-099ff8e9086e is not authorized to perform: ecr:GetAuthorizationToke
Fix:
Update the code to image: ecs.ContainerImage.fromEcrRepository(myRepository, 'latest')
Error: An error occurred (AccessDenied) when calling the ReceiveMessage operation: Access to the resource https://cn-north-1.queue.amazonaws.com.cn/ is denied.
Fix:
Update the code to mySQSqueue.grantConsumeMessages(myQueueProcessingService.taskDefinition.taskRole);
You need run the npm install @aws-cdk/aws-{package1, package2 ...} to upgrade the version