Create an aws cdk script to create an sqs queue and a lambda associated to it
- User
Create an aws cdk script to create an sqs queue and a lambda associated to it
- Assistant
- User
Can you write it in Python please?
- Assistant
Sure, here's an example of how to create an SQS queue and a Lambda function associated with it using Python and AWS CDK:
- User
Can you add a deadletter to the queue please?
- Assistant
- User
the lambda cannot be found
- Assistant
You are absolutely right! I apologize for the oversight in the previous example. The Lambda function needs to be properly referenced in the
environmentvariable of theSqsEventSource.Here's the corrected code:
import aws_cdk as cdk from aws_cdk import aws_lambda as lambda_ from aws_cdk import aws_sqs as sqs from aws_cdk import aws_lambda_event_sources as lambda_event_sources class SqsLambdaStack(cdk.Stack): def __init__(self, scope: cdk.Construct, id: str, **kwargs) -> None: super().__init__(scope, id, **kwargs) # Create an SQS queue queue = sqs.Queue(self, 'MyQueue', visibility_timeout=cdk.Duration.seconds(300), delay_seconds=10, retention_period=cdk.Duration.days(7), ) # Create a deadletter queue deadletter_queue = sqs.Queue(self, 'DeadLetterQueue', visibility_timeout=cdk.Duration.seconds(300), delay_seconds=10, retention_period=cdk.Duration.days(7), ) # Create a Lambda function lambda_function = lambda_.Function(self, 'MyLambdaFunction', runtime=lambda_.Runtime.NODEJS_14_X, code=lambda_.Code.from_asset('lambda'), handler='index.handler', ) # Add the SQS queue as an event source for the Lambda function lambda_function.add_event_source(lambda_event_sources.SqsEventSource(queue, deadletter_queue=deadletter_queue))
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.