I am new to AWS CDK and TypeScript. Have a general question related to typescript in context with AWS CDK. I tried to find a similar example or question being asked in stackoverflow but could not find. In the code snippet below, what does this
refer to in terms of context?
// An sqs queue for unsuccessful invocations of a lambda function
import * as sqs from 'aws-cdk-lib/aws-sqs';
const deadLetterQueue = new sqs.Queue(this, 'DeadLetterQueue');
const myFn = new lambda.Function(this, 'Fn', {
runtime: lambda.Runtime.NODEJS_14_X,
handler: 'index.handler',
code: lambda.Code.fromInline('// your code'),
// sqs queue for unsuccessful invocations
onFailure: new destinations.SqsDestination(deadLetterQueue),
});
https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_sqs.Queue.html
CodePudding user response:
The sample code is incomplete. This type of code is found in the context of a Construct
. A more plausible version would be something like this:
// An sqs queue for unsuccessful invocations of a lambda function
import * as sqs from 'aws-cdk-lib/aws-sqs';
export class MyConstruct extends Construct {
constructor(scope: Construct, id: string) {
super(scope, id);
const deadLetterQueue = new sqs.Queue(this, 'DeadLetterQueue');
const myFn = new lambda.Function(this, 'Fn', {
runtime: lambda.Runtime.NODEJS_14_X,
handler: 'index.handler',
code: lambda.Code.fromInline('// your code'),
// sqs queue for unsuccessful invocations
onFailure: new destinations.SqsDestination(deadLetterQueue),
});
}
}
// https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_sqs.Queue.html
this
then references the construct. This pattern is seen all CDK code. MyConstruct will probably be referenced inside a stack:
new MyConstruct(this, 'my-construct');
Here, 'this' would reference the Stack
it's in.