-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Issue#822 - API Gateway Async Lambda Invocation #1089
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9bd640a
initial commit
6f53465
updated README.md
bcd73ab
added architecture diagram
b2a901c
Merge branch 'main' into issue#822
fanq10 faa8f8e
added background and solution in README.md
jfan9 856de10
Merge branch 'main' into issue#822
kaiz-io cf0d3ff
Delete typescript/api-gateway-async-lambda-invocation/test/api-gatewa…
kaiz-io 3f9c8bb
Delete typescript/api-gateway-async-lambda-invocation/jest.config.js
kaiz-io e9407ad
Update package.json
kaiz-io 6e019b5
Update package.json
kaiz-io 4094f53
Merge branch 'main' into issue#822
kaiz-io 26b5bc3
added lambda handler: job_handler.js
jfan9 e6315e9
Merge branch 'main' into issue#822
jfan9 de5e444
Merge branch 'main' into issue#822
kaiz-io File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| *.js | ||
| !jest.config.js | ||
| *.d.ts | ||
| node_modules | ||
| package-lock.json | ||
|
|
||
| # CDK asset staging directory | ||
| .cdk.staging | ||
| cdk.out |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| *.ts | ||
| !*.d.ts | ||
|
|
||
| # CDK asset staging directory | ||
| .cdk.staging | ||
| cdk.out |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # API Gateway Asynchronous Lambda Invocation | ||
|
|
||
| Sample architecture to process events asynchronously using API Gateway and Lambda and store result in DynamoDB. | ||
|
|
||
| ## Architecture | ||
|  | ||
|
|
||
| ## Background: | ||
|
|
||
| In Lambda non-proxy (custom) integration, the backend Lambda function is invoked synchronously by default. This is the desired behavior for most REST API operations. | ||
| Some applications, however, require work to be performed asynchronously (as a batch operation or a long-latency operation), typically by a separate backend component. | ||
| In this case, the backend Lambda function is invoked asynchronously, and the front-end REST API method doesn't return the result. | ||
|
|
||
| ## Solution: | ||
|
|
||
| ### API Gateway: | ||
|
|
||
| - `POST` `/job`: Integrates with the Lambda function for job submission. | ||
| - `GET` `/job/{jobId}`: Direct DynamoDB integration to fetch the job status by jobId. | ||
|
|
||
| ### DynamoDB Integration: | ||
|
|
||
| - The stack includes the DynamoDB table for storing job statuses with jobId as the partition key. | ||
| - The Lambda function has permissions to write to the DynamoDB table. | ||
|
|
||
| ### IAM Role: | ||
| - An IAM role is created for API Gateway with permissions to access the DynamoDB table for the GET /job/{jobId} method | ||
|
|
||
| ### Example structure: | ||
| ``` | ||
| /api-gateway-async-lambda-invocation | ||
| ├── /assets | ||
| │ └── /lambda-functions | ||
| │ └── job_handler.js | ||
| ├── /lib | ||
| | |-- app.ts | ||
| │ └── api-gateway-async-lambda-invocation-stack.ts | ||
| ├── node_modules | ||
| ├── package.json | ||
| ├── cdk.json | ||
| └── ... | ||
| ``` | ||
|
|
||
| ## Test: | ||
| - `POST` curl command: | ||
| ```shell | ||
| curl -X POST https://<API-ID>.execute-api.<REGION>.amazonaws.com/<stage>/job \ | ||
| -H "X-Amz-Invocation-Type: Event" \ | ||
| -H "Content-Type: application/json" \ | ||
| -d '{}' | ||
| ``` | ||
|
|
||
| - `GET` curl command to get job details: | ||
| ```shell | ||
| # jobId refers the output of the POST curl command. | ||
| curl https://<API-ID>.execute-api.<REGION>.amazonaws.com/<stage>/job/<jobId> | ||
| ``` | ||
|
|
||
| ## Reference: | ||
| [1] Set up asynchronous invocation of the backend Lambda function | ||
| https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-integration-async.html |
45 changes: 45 additions & 0 deletions
45
typescript/api-gateway-async-lambda-invocation/assets/lambda-functions/job_handler.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| // Import necessary modules from AWS SDK v3 | ||
| const { DynamoDBClient } = require('@aws-sdk/client-dynamodb'); | ||
| const { PutCommand } = require('@aws-sdk/lib-dynamodb'); // Import PutCommand | ||
|
|
||
| // Create a DynamoDB client | ||
| const dynamoDBClient = new DynamoDBClient({}); | ||
|
|
||
| exports.handler = async (event) => { | ||
| const jobId = event.jobId; | ||
| const status = 'Processed'; // Initial job status | ||
| const createdAt = new Date().toISOString(); // Current timestamp | ||
|
|
||
| // Job item to be saved in DynamoDB | ||
| const jobItem = { | ||
| jobId, | ||
| status, | ||
| createdAt, | ||
| }; | ||
|
|
||
| const params = { | ||
| TableName: process.env.JOB_TABLE, | ||
| Item: jobItem, | ||
| }; | ||
|
|
||
| try { | ||
| // Insert the job into the DynamoDB table | ||
| const command = new PutCommand(params); | ||
| await dynamoDBClient.send(command); | ||
|
|
||
| // Return the jobId to the client immediately | ||
| const response = { | ||
| statusCode: 200, | ||
| body: JSON.stringify({ jobId }), // Return jobId to the client | ||
| }; | ||
|
|
||
| // Return jobId immediately | ||
| return response; | ||
| } catch (error) { | ||
| console.error('Error processing job:', error); | ||
| return { | ||
| statusCode: 500, | ||
| body: JSON.stringify({ error: 'Could not process job' }), | ||
| }; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| { | ||
| "app": "npx ts-node --prefer-ts-exts lib/app.ts", | ||
| "watch": { | ||
| "include": [ | ||
| "**" | ||
| ], | ||
| "exclude": [ | ||
| "README.md", | ||
| "cdk*.json", | ||
| "**/*.d.ts", | ||
| "**/*.js", | ||
| "tsconfig.json", | ||
| "package*.json", | ||
| "yarn.lock", | ||
| "node_modules", | ||
| "test" | ||
| ] | ||
| }, | ||
| "context": { | ||
| "@aws-cdk/aws-lambda:recognizeLayerVersion": true, | ||
| "@aws-cdk/core:checkSecretUsage": true, | ||
| "@aws-cdk/core:target-partitions": [ | ||
| "aws", | ||
| "aws-cn" | ||
| ], | ||
| "@aws-cdk-containers/ecs-service-extensions:enableDefaultLogDriver": true, | ||
| "@aws-cdk/aws-ec2:uniqueImdsv2TemplateName": true, | ||
| "@aws-cdk/aws-ecs:arnFormatIncludesClusterName": true, | ||
| "@aws-cdk/aws-iam:minimizePolicies": true, | ||
| "@aws-cdk/core:validateSnapshotRemovalPolicy": true, | ||
| "@aws-cdk/aws-codepipeline:crossAccountKeyAliasStackSafeResourceName": true, | ||
| "@aws-cdk/aws-s3:createDefaultLoggingPolicy": true, | ||
| "@aws-cdk/aws-sns-subscriptions:restrictSqsDescryption": true, | ||
| "@aws-cdk/aws-apigateway:disableCloudWatchRole": true, | ||
| "@aws-cdk/core:enablePartitionLiterals": true, | ||
| "@aws-cdk/aws-events:eventsTargetQueueSameAccount": true, | ||
| "@aws-cdk/aws-ecs:disableExplicitDeploymentControllerForCircuitBreaker": true, | ||
| "@aws-cdk/aws-iam:importedRoleStackSafeDefaultPolicyName": true, | ||
| "@aws-cdk/aws-s3:serverAccessLogsUseBucketPolicy": true, | ||
| "@aws-cdk/aws-route53-patters:useCertificate": true, | ||
| "@aws-cdk/customresources:installLatestAwsSdkDefault": false, | ||
| "@aws-cdk/aws-rds:databaseProxyUniqueResourceName": true, | ||
| "@aws-cdk/aws-codedeploy:removeAlarmsFromDeploymentGroup": true, | ||
| "@aws-cdk/aws-apigateway:authorizerChangeDeploymentLogicalId": true, | ||
| "@aws-cdk/aws-ec2:launchTemplateDefaultUserData": true, | ||
| "@aws-cdk/aws-secretsmanager:useAttachedSecretResourcePolicyForSecretTargetAttachments": true, | ||
| "@aws-cdk/aws-redshift:columnId": true, | ||
| "@aws-cdk/aws-stepfunctions-tasks:enableEmrServicePolicyV2": true, | ||
| "@aws-cdk/aws-ec2:restrictDefaultSecurityGroup": true, | ||
| "@aws-cdk/aws-apigateway:requestValidatorUniqueId": true, | ||
| "@aws-cdk/aws-kms:aliasNameRef": true, | ||
| "@aws-cdk/aws-autoscaling:generateLaunchTemplateInsteadOfLaunchConfig": true, | ||
| "@aws-cdk/core:includePrefixInUniqueNameGeneration": true, | ||
| "@aws-cdk/aws-efs:denyAnonymousAccess": true, | ||
| "@aws-cdk/aws-opensearchservice:enableOpensearchMultiAzWithStandby": true, | ||
| "@aws-cdk/aws-lambda-nodejs:useLatestRuntimeVersion": true, | ||
| "@aws-cdk/aws-efs:mountTargetOrderInsensitiveLogicalId": true, | ||
| "@aws-cdk/aws-rds:auroraClusterChangeScopeOfInstanceParameterGroupWithEachParameters": true, | ||
| "@aws-cdk/aws-appsync:useArnForSourceApiAssociationIdentifier": true, | ||
| "@aws-cdk/aws-rds:preventRenderingDeprecatedCredentials": true, | ||
| "@aws-cdk/aws-codepipeline-actions:useNewDefaultBranchForCodeCommitSource": true, | ||
| "@aws-cdk/aws-cloudwatch-actions:changeLambdaPermissionLogicalIdForLambdaAction": true, | ||
| "@aws-cdk/aws-codepipeline:crossAccountKeysDefaultValueToFalse": true, | ||
| "@aws-cdk/aws-codepipeline:defaultPipelineTypeToV2": true, | ||
| "@aws-cdk/aws-kms:reduceCrossAccountRegionPolicyScope": true, | ||
| "@aws-cdk/aws-eks:nodegroupNameAttribute": true, | ||
| "@aws-cdk/aws-ec2:ebsDefaultGp3Volume": true, | ||
| "@aws-cdk/aws-ecs:removeDefaultDeploymentAlarm": true, | ||
| "@aws-cdk/custom-resources:logApiResponseDataPropertyTrueDefault": false, | ||
| "@aws-cdk/aws-s3:keepNotificationInImportedBucket": false, | ||
| "@aws-cdk/aws-ecs:reduceEc2FargateCloudWatchPermissions": true, | ||
| "@aws-cdk/aws-ec2:ec2SumTImeoutEnabled": true, | ||
| "@aws-cdk/aws-appsync:appSyncGraphQLAPIScopeLambdaPermission": true, | ||
| "@aws-cdk/aws-rds:setCorrectValueForDatabaseInstanceReadReplicaInstanceResourceId": true, | ||
| "@aws-cdk/core:cfnIncludeRejectComplexResourceUpdateCreatePolicyIntrinsics": true, | ||
| "@aws-cdk/aws-lambda-nodejs:sdkV3ExcludeSmithyPackages": true | ||
| } | ||
| } |
Binary file added
BIN
+27.7 KB
typescript/api-gateway-async-lambda-invocation/images/architecture.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
170 changes: 170 additions & 0 deletions
170
...ript/api-gateway-async-lambda-invocation/lib/api-gateway-async-lambda-invocation-stack.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| import * as cdk from 'aws-cdk-lib'; | ||
| import { AccessLogFormat, AwsIntegration, LambdaIntegration, LambdaRestApi, LogGroupLogDestination, MethodLoggingLevel } from 'aws-cdk-lib/aws-apigateway'; | ||
| import { AttributeType, Table } from 'aws-cdk-lib/aws-dynamodb'; | ||
| import { PolicyDocument, PolicyStatement, Role, ServicePrincipal } from 'aws-cdk-lib/aws-iam'; | ||
| import { Code, Function, Runtime } from 'aws-cdk-lib/aws-lambda'; | ||
| import { LogGroup, RetentionDays } from 'aws-cdk-lib/aws-logs'; | ||
| import { Construct } from 'constructs'; | ||
| import path = require('path'); | ||
|
|
||
| export interface Properties extends cdk.StackProps { | ||
| readonly prefix: string; | ||
| } | ||
|
|
||
| export class ApiGatewayAsyncLambdaStack extends cdk.Stack { | ||
| constructor(scope: Construct, id: string, props: Properties) { | ||
| super(scope, id, props); | ||
|
|
||
| // DynamoDB table for job status | ||
| const jobTable = new Table(this, `${props.prefix}-table`, { | ||
| partitionKey: { name: 'jobId', type: AttributeType.STRING }, | ||
| tableName: `${props.prefix}-job-table`, | ||
| removalPolicy: cdk.RemovalPolicy.DESTROY, // NOT recommended for production; Set `cdk.RemovalPolicy.RETAIN` for production | ||
| }); | ||
|
|
||
| // Create a Log Group for API Gateway logs | ||
| const fnLogGroup = new LogGroup(this, `${props.prefix}-fn-log-group`, { | ||
| retention: RetentionDays.ONE_WEEK, // Customize the retention period as needed | ||
| }); | ||
|
|
||
| // create a lambda function | ||
| const jobHandler = new Function(this, `${props.prefix}-fn`, { | ||
| runtime: Runtime.NODEJS_20_X, | ||
| handler: 'job_handler.handler', | ||
| code: Code.fromAsset(path.join(__dirname, '../assets/lambda-functions')), | ||
| environment: { | ||
| JOB_TABLE: jobTable.tableName, | ||
| }, | ||
| logGroup: fnLogGroup, | ||
| }); | ||
| // Grant Lambda permission to write to DynamoDB | ||
| jobTable.grantWriteData(jobHandler); | ||
|
|
||
| // Create a Log Group for API Gateway logs | ||
| const apiLogGroup = new LogGroup(this, `${props.prefix}-apigw-log-group`, { | ||
| retention: RetentionDays.ONE_WEEK, // Customize the retention period as needed | ||
| }); | ||
|
|
||
| // API Gateway: Create a REST API with Lambda integration for POST /job | ||
| const api = new LambdaRestApi(this, `${props.prefix}-apigw`, { | ||
| restApiName: `${props.prefix}-job-service`, | ||
| handler: jobHandler, | ||
| proxy: false, | ||
| cloudWatchRole: true, | ||
| deployOptions: { | ||
| metricsEnabled: true, | ||
| dataTraceEnabled: true, | ||
| accessLogDestination: new LogGroupLogDestination(apiLogGroup), | ||
| accessLogFormat: AccessLogFormat.jsonWithStandardFields(), | ||
| loggingLevel: MethodLoggingLevel.ERROR, | ||
| } | ||
| }); | ||
|
|
||
| // POST /job method (Lambda integration) | ||
| const job = api.root.addResource('job') | ||
|
|
||
| // POST /job method with asynchronous invocation | ||
| job.addMethod("POST", | ||
| new LambdaIntegration(jobHandler,{ | ||
| proxy:false, | ||
| requestParameters:{ | ||
| 'integration.request.header.X-Amz-Invocation-Type': "'Event'", | ||
| }, | ||
| requestTemplates: { | ||
| 'application/json': `{ | ||
| "jobId": "$context.requestId", | ||
| "body": $input.json('$') | ||
| }`, | ||
| }, | ||
| integrationResponses: [ | ||
| { | ||
| statusCode: '200', | ||
| responseTemplates: { | ||
| 'application/json': `{"jobId": "$context.requestId"}` | ||
| } | ||
| }, | ||
| { | ||
| statusCode: '500', | ||
| responseTemplates: { | ||
| 'application/json': `{ | ||
| "error": "An error occurred while processing the request.", | ||
| "details": "$context.integrationErrorMessage" | ||
| }` | ||
| } | ||
| } | ||
| ] | ||
| }), | ||
| { | ||
| methodResponses: [ | ||
| { | ||
| statusCode: '200', | ||
| }, | ||
| { | ||
| statusCode: '500', | ||
| } | ||
| ] | ||
| } | ||
| ); | ||
|
|
||
| // GET method to check the status of a job by jobId (direct DynamoDB integration) | ||
| const jobId = job.addResource('{jobId}'); | ||
| jobId.addMethod("GET", | ||
| new AwsIntegration({ | ||
| service: 'dynamodb', | ||
| action: 'GetItem', | ||
| options: { | ||
| credentialsRole: new Role(this, 'ApiGatewayDynamoRole',{ | ||
| assumedBy: new ServicePrincipal('apigateway.amazonaws.com'), | ||
| inlinePolicies: { | ||
| dynamoPolicy: new PolicyDocument({ | ||
| statements: [ | ||
| new PolicyStatement({ | ||
| actions: ['dynamodb:GetItem'], | ||
| resources: [jobTable.tableArn], | ||
| }), | ||
| ], | ||
| }) | ||
| } | ||
| }), | ||
| requestTemplates: { | ||
| 'application/json': `{ | ||
| "TableName": "${jobTable.tableName}", | ||
| "Key": { | ||
| "jobId": { | ||
| "S": "$input.params('jobId')" | ||
| } | ||
| } | ||
| }`, | ||
| }, | ||
| integrationResponses: [{ | ||
| statusCode: '200', | ||
| responseTemplates: { | ||
| 'application/json': `{ | ||
| "jobId": "$input.path('$.Item.jobId.S')", | ||
| "status": "$input.path('$.Item.status.S')", | ||
| "createdAt": "$input.path('$.Item.createdAt.S')" | ||
| }` | ||
| } | ||
| }, | ||
| { | ||
| statusCode: '404', | ||
| selectionPattern: '.*"Item":null.*', | ||
| responseTemplates: { | ||
| 'application/json': '{"error": "Job not found"}' | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| }), | ||
| { | ||
| methodResponses:[ | ||
| { | ||
| statusCode: '200' | ||
| }, | ||
| { | ||
| statusCode: '404' | ||
| } | ||
| ] | ||
| }); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| #!/usr/bin/env node | ||
| import 'source-map-support/register'; | ||
| import * as cdk from 'aws-cdk-lib'; | ||
| import { ApiGatewayAsyncLambdaStack } from '../lib/api-gateway-async-lambda-invocation-stack'; | ||
|
|
||
| const app = new cdk.App(); | ||
| const env = { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION } | ||
| const prefix = 'apigw-async-lambda'; | ||
|
|
||
| const apigw_async_lambda = new ApiGatewayAsyncLambdaStack(app, 'ApiGatewayAsyncLambdaStack', { | ||
| env, | ||
| stackName: `${prefix}-stack`, | ||
| prefix, | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.