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
Sure, here's an AWS CDK script in TypeScript to create an SQS queue and a Lambda function associated with it:
import * as cdk from 'aws-cdk-lib'; import * as sqs from 'aws-cdk-lib/aws-sqs'; import * as lambda from 'aws-cdk-lib/aws-lambda'; import * as iam from 'aws-cdk-lib/aws-iam'; import { Duration } from 'aws-cdk-lib'; export class SqsLambdaStack extends cdk.Stack { constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // Create an SQS queue const queue = new sqs.Queue(this, 'MyQueue', { queueName: 'my-queue' }); // Create a Lambda function const lambdaFunction = new lambda.Function(this, 'MyFunction', { functionName: 'my-function', runtime: lambda.Runtime.NODEJS_14_X, handler: 'index.handler', code: lambda.Code.fromAsset('lambda'), timeout: Duration.seconds(10), environment: { QUEUE_URL: queue.queueUrl } }); // Grant permissions to the Lambda function to access the SQS queue queue.grantSendMessages(lambdaFunction); // Add an inline policy to the Lambda function's execution role to allow it to write to CloudWatch Logs lambdaFunction.addToRolePolicy(new iam.PolicyStatement({ actions: ['logs:CreateLogGroup', 'logs:CreateLogStream', 'logs:PutLogEvents'], resources: ['arn:aws:logs:*:*:*'] })); } } // Instantiate the stack const app = new cdk.App(); new SqsLambdaStack(app, 'MyStack'); - User
Can you write it in Python please?
- Assistant
Yes, certainly! Here's the same script written in Python:
from aws_cdk import ( core, aws_sqs as sqs, aws_lambda as _lambda, aws_iam as iam, Duration ) class SqsLambdaStack(core.Stack): def __init__(self, scope: core.Construct, id: str, **kwargs) -> None: super().__init__(scope, id, **kwargs) # Create an SQS queue queue = sqs.Queue( self, 'MyQueue', queue_name='my-queue' ) # Create a Lambda function lambda_function = _lambda.Function( self, 'MyFunction', function_name='my-function', runtime=_lambda.Runtime.PYTHON_3_8, handler='index.handler', code=_lambda.Code.from_asset('lambda'), timeout=Duration.seconds(10), environment={ 'QUEUE_URL': queue.queue_url } ) # Grant permissions to the Lambda function to access the SQS queue queue.grant_send_messages(lambda_function) # Add an inline policy to the Lambda function's execution role to allow it to write to CloudWatch Logs lambda_function.add_to_role_policy(iam.PolicyStatement( actions=['logs:CreateLogGroup', 'logs:CreateLogStream', 'logs:PutLogEvents'], resources=['arn:aws:logs:*:*:*'] )) # Instantiate the stack app = core.App() SqsLambdaStack(app, 'MyStack') - User
Can you add a deadletter to the queue please?
- Assistant
Sure, here's an updated Python script that creates an SQS queue with a dead letter queue (DLQ):
from aws_cdk import ( core, aws_sqs as sqs, aws_lambda as _lambda, aws_iam as iam, Duration ) class SqsLambdaStack(core.Stack): def __init__(self, scope: core.Construct, id: str, **kwargs) -> None: super().__init__(scope, id, **kwargs) # Create a dead letter queue dlq = sqs.Queue( self, 'MyDLQ', queue_name='my-queue-dlq' ) # Create an SQS queue with the dead letter queue queue = sqs.Queue( self, 'MyQueue', queue_name='my-queue', dead_letter_queue=sqs.DeadLetterQueue( max_receive_count=3, queue=dlq ) ) # Create a Lambda function lambda_function = _lambda.Function( self, 'MyFunction', function_name='my-function', runtime=_lambda.Runtime.PYTHON_3_8, handler='index.handler', code=_lambda.Code.from_asset('lambda'), timeout=Duration.seconds(10), environment={ 'QUEUE_URL': queue.queue_url } ) # Grant permissions to the Lambda function to access the SQS queue queue.grant_send_messages(lambda_function) # Add an inline policy to the Lambda function's execution role to allow it to write to CloudWatch Logs lambda_function.add_to_role_policy(iam.PolicyStatement( actions=['logs:CreateLogGroup', 'logs:CreateLogStream', 'logs:PutLogEvents'], resources=['arn:aws:logs:*:*:*'] )) # Instantiate the stack app = core.App() SqsLambdaStack(app, 'MyStack')
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.