Skip to content

Instantly share code, notes, and snippets.

@asanka-x
Last active October 7, 2019 03:49
Show Gist options
  • Save asanka-x/464fff2d8322f63c03a30ca707b941a8 to your computer and use it in GitHub Desktop.
Save asanka-x/464fff2d8322f63c03a30ca707b941a8 to your computer and use it in GitHub Desktop.
cloud formation template to provision asynchronous api using API Gateway, SQS and Lambda
Resources:
asyncApiMessageDLQ23A9B11D:
Type: AWS::SQS::Queue
Metadata:
aws:cdk:path: AwsAsyncApiWithCdkStack/asyncApiMessageDLQ/Resource
asyncApiMessageQueue8EE5ED5B:
Type: AWS::SQS::Queue
Properties:
RedrivePolicy:
deadLetterTargetArn:
Fn::GetAtt:
- asyncApiMessageDLQ23A9B11D
- Arn
maxReceiveCount: 3
Metadata:
aws:cdk:path: AwsAsyncApiWithCdkStack/asyncApiMessageQueue/Resource
asyncApiApigRole9BA470E6:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service: apigateway.amazonaws.com
Version: "2012-10-17"
Metadata:
aws:cdk:path: AwsAsyncApiWithCdkStack/asyncApiApigRole/Resource
asyncApiApigRoleDefaultPolicy0C168878:
Type: AWS::IAM::Policy
Properties:
PolicyDocument:
Statement:
- Action: sqs:SendMessage
Effect: Allow
Resource:
Fn::GetAtt:
- asyncApiMessageQueue8EE5ED5B
- Arn
- Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:DescribeLogGroups
- logs:DescribeLogStreams
- logs:PutLogEvents
- logs:GetLogEvents
- logs:FilterLogEvents
Effect: Allow
Resource: "*"
Version: "2012-10-17"
PolicyName: asyncApiApigRoleDefaultPolicy0C168878
Roles:
- Ref: asyncApiApigRole9BA470E6
Metadata:
aws:cdk:path: AwsAsyncApiWithCdkStack/asyncApiApigRole/DefaultPolicy/Resource
asyncApi7BFD70F3:
Type: AWS::ApiGateway::RestApi
Properties:
EndpointConfiguration:
Types:
- PRIVATE
Name: asyncApi
Policy:
Statement:
- Action: execute-api:Invoke
Condition:
StringEquals:
aws:sourceVpc: vpc-xxxxxxxxxx
Effect: Allow
Principal: "*"
Resource: "*"
Version: "2012-10-17"
Metadata:
aws:cdk:path: AwsAsyncApiWithCdkStack/asyncApi/Resource
asyncApiDeploymentA88CF396752db9324cb7ed8b94f155363f47f804:
Type: AWS::ApiGateway::Deployment
Properties:
RestApiId:
Ref: asyncApi7BFD70F3
Description: Automatically created by the RestApi construct
DependsOn:
- asyncApiasyncendpointPOSTE58E4D27
- asyncApiasyncendpointD7E7F6E9
Metadata:
aws:cdk:path: AwsAsyncApiWithCdkStack/asyncApi/Deployment/Resource
asyncApiDeploymentStageprod1357F97A:
Type: AWS::ApiGateway::Stage
Properties:
RestApiId:
Ref: asyncApi7BFD70F3
DeploymentId:
Ref: asyncApiDeploymentA88CF396752db9324cb7ed8b94f155363f47f804
MethodSettings:
- DataTraceEnabled: true
HttpMethod: "*"
LoggingLevel: INFO
ResourcePath: /*
StageName: prod
Metadata:
aws:cdk:path: AwsAsyncApiWithCdkStack/asyncApi/DeploymentStage.prod/Resource
asyncApiCloudWatchRoleED6EC42D:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service: apigateway.amazonaws.com
Version: "2012-10-17"
ManagedPolicyArns:
- Fn::Join:
- ""
- - "arn:"
- Ref: AWS::Partition
- :iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs
Metadata:
aws:cdk:path: AwsAsyncApiWithCdkStack/asyncApi/CloudWatchRole/Resource
asyncApiAccount2053A13E:
Type: AWS::ApiGateway::Account
Properties:
CloudWatchRoleArn:
Fn::GetAtt:
- asyncApiCloudWatchRoleED6EC42D
- Arn
DependsOn:
- asyncApi7BFD70F3
Metadata:
aws:cdk:path: AwsAsyncApiWithCdkStack/asyncApi/Account
asyncApiasyncendpointD7E7F6E9:
Type: AWS::ApiGateway::Resource
Properties:
ParentId:
Fn::GetAtt:
- asyncApi7BFD70F3
- RootResourceId
PathPart: async_endpoint
RestApiId:
Ref: asyncApi7BFD70F3
Metadata:
aws:cdk:path: AwsAsyncApiWithCdkStack/asyncApi/Default/async_endpoint/Resource
asyncApiasyncendpointPOSTE58E4D27:
Type: AWS::ApiGateway::Method
Properties:
HttpMethod: POST
ResourceId:
Ref: asyncApiasyncendpointD7E7F6E9
RestApiId:
Ref: asyncApi7BFD70F3
AuthorizationType: NONE
Integration:
Credentials:
Fn::GetAtt:
- asyncApiApigRole9BA470E6
- Arn
IntegrationHttpMethod: POST
IntegrationResponses:
- ResponseTemplates:
text/html: Success
StatusCode: "200"
- ResponseTemplates:
text/html: Error
SelectionPattern: "500"
StatusCode: "500"
PassthroughBehavior: NEVER
RequestParameters:
integration.request.header.Content-Type: "'application/x-www-form-urlencoded'"
RequestTemplates:
application/x-www-form-urlencoded: Action=SendMessage&MessageBody=$util.urlEncode("$input.body")&MessageAttribute.1.Name=queryParam1&MessageAttribute.1.Value.StringValue=$input.params("query_param_1")&MessageAttribute.1.Value.DataType=String
Type: AWS
Uri:
Fn::Join:
- ""
- - "arn:"
- Ref: AWS::Partition
- :apigateway:us-east-1:sqs:path/XXXXXXXX/
- Fn::GetAtt:
- asyncApiMessageQueue8EE5ED5B
- QueueName
MethodResponses:
- ResponseParameters:
method.response.header.Content-Type: true
StatusCode: "200"
- ResponseParameters:
method.response.header.Content-Type: true
StatusCode: "500"
RequestParameters:
method.request.querystring.query_param_1: true
Metadata:
aws:cdk:path: AwsAsyncApiWithCdkStack/asyncApi/Default/async_endpoint/POST/Resource
asyncProcessFuncServiceRoleDFBA8C4D:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service: lambda.amazonaws.com
Version: "2012-10-17"
ManagedPolicyArns:
- Fn::Join:
- ""
- - "arn:"
- Ref: AWS::Partition
- :iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Metadata:
aws:cdk:path: AwsAsyncApiWithCdkStack/asyncProcessFunc/ServiceRole/Resource
asyncProcessFuncServiceRoleDefaultPolicy5CF508B3:
Type: AWS::IAM::Policy
Properties:
PolicyDocument:
Statement:
- Action:
- sqs:ReceiveMessage
- sqs:ChangeMessageVisibility
- sqs:GetQueueUrl
- sqs:DeleteMessage
- sqs:GetQueueAttributes
Effect: Allow
Resource:
Fn::GetAtt:
- asyncApiMessageQueue8EE5ED5B
- Arn
Version: "2012-10-17"
PolicyName: asyncProcessFuncServiceRoleDefaultPolicy5CF508B3
Roles:
- Ref: asyncProcessFuncServiceRoleDFBA8C4D
Metadata:
aws:cdk:path: AwsAsyncApiWithCdkStack/asyncProcessFunc/ServiceRole/DefaultPolicy/Resource
asyncProcessFuncDA2FDD8B:
Type: AWS::Lambda::Function
Properties:
Code:
S3Bucket:
Ref: asyncProcessFuncCodeS3Bucket337D387B
S3Key:
Fn::Join:
- ""
- - Fn::Select:
- 0
- Fn::Split:
- "||"
- Ref: asyncProcessFuncCodeS3VersionKey95BF6690
- Fn::Select:
- 1
- Fn::Split:
- "||"
- Ref: asyncProcessFuncCodeS3VersionKey95BF6690
Handler: index.handler
Role:
Fn::GetAtt:
- asyncProcessFuncServiceRoleDFBA8C4D
- Arn
Runtime: nodejs10.x
DependsOn:
- asyncProcessFuncServiceRoleDefaultPolicy5CF508B3
- asyncProcessFuncServiceRoleDFBA8C4D
Metadata:
aws:cdk:path: AwsAsyncApiWithCdkStack/asyncProcessFunc/Resource
aws:asset:path: asset.6c2a34e68f7802ac4d5e1be44927774ccabcf40c58ac0b5408e055631fa78685
aws:asset:property: Code
asyncProcessFuncSqsEventSourceAwsAsyncApiWithCdkStackasyncApiMessageQueue66B16199E519E403:
Type: AWS::Lambda::EventSourceMapping
Properties:
EventSourceArn:
Fn::GetAtt:
- asyncApiMessageQueue8EE5ED5B
- Arn
FunctionName:
Ref: asyncProcessFuncDA2FDD8B
Metadata:
aws:cdk:path: AwsAsyncApiWithCdkStack/asyncProcessFunc/SqsEventSource:AwsAsyncApiWithCdkStackasyncApiMessageQueue66B16199/Resource
CDKMetadata:
Type: AWS::CDK::Metadata
Properties:
Modules: aws-cdk=1.11.0,@aws-cdk/assets=1.11.0,@aws-cdk/aws-apigateway=1.11.0,@aws-cdk/aws-applicationautoscaling=1.11.0,@aws-cdk/aws-autoscaling-common=1.11.0,@aws-cdk/aws-cloudwatch=1.11.0,@aws-cdk/aws-dynamodb=1.11.0,@aws-cdk/aws-ec2=1.11.0,@aws-cdk/aws-events=1.11.0,@aws-cdk/aws-iam=1.11.0,@aws-cdk/aws-kms=1.11.0,@aws-cdk/aws-lambda=1.11.0,@aws-cdk/aws-lambda-event-sources=1.11.0,@aws-cdk/aws-s3=1.11.0,@aws-cdk/aws-s3-assets=1.11.0,@aws-cdk/aws-s3-notifications=1.11.0,@aws-cdk/aws-sns=1.11.0,@aws-cdk/aws-sns-subscriptions=1.11.0,@aws-cdk/aws-sqs=1.11.0,@aws-cdk/aws-ssm=1.11.0,@aws-cdk/core=1.11.0,@aws-cdk/cx-api=1.11.0,@aws-cdk/region-info=1.11.0,jsii-runtime=node.js/v10.16.0
Outputs:
asyncApiEndpointEE7368EE:
Value:
Fn::Join:
- ""
- - https://
- Ref: asyncApi7BFD70F3
- .execute-api.us-east-1.
- Ref: AWS::URLSuffix
- /
- Ref: asyncApiDeploymentStageprod1357F97A
- /
Parameters:
asyncProcessFuncCodeS3Bucket337D387B:
Type: String
Description: S3 bucket for asset "AwsAsyncApiWithCdkStack/asyncProcessFunc/Code"
asyncProcessFuncCodeS3VersionKey95BF6690:
Type: String
Description: S3 key for asset version "AwsAsyncApiWithCdkStack/asyncProcessFunc/Code"
asyncProcessFuncCodeArtifactHashD27D9BD5:
Type: String
Description: Artifact hash for asset "AwsAsyncApiWithCdkStack/asyncProcessFunc/Code"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment