gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
Create a cdk with the following 1. Trigger a sns when lambda receives a message 2. SNS…
- User
Create a cdk with the following
- Trigger a sns when lambda receives a message
- SNS sends an email
- Include cloudwatch logs for observability
- Assistant
Here's a sample CDK code that creates a Lambda function, a SNS topic, and a CloudWatch log group, and links them together:
import * as cdk from 'aws-cdk-lib'; import * as sns from 'aws-cdk-lib/aws-sns'; import * as lambda from 'aws-cdk-lib/aws-lambda'; import * as logs from 'aws-cdk-lib/aws-logs'; import * as iam from 'aws-cdk-lib/aws-iam'; import * as path from 'path'; // Define the stack export class MyStack extends cdk.Stack { constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); // Create the log group const logGroup = new logs.LogGroup(this, 'LogGroup', { logGroupName: '/aws/lambda/MyFunction' }); // Create the SNS topic const topic = new sns.Topic(this, 'Topic'); // Create the Lambda function const handler = new lambda.Function(this, 'MyFunction', { code: lambda.Code.fromAsset(path.join(__dirname, '../lambda')), runtime: lambda.Runtime.NODEJS_14_X, handler: 'index.handler', logRetention: logs.RetentionDays.ONE_WEEK, environment: { TOPIC_ARN: topic.topicArn } }); // Add permissions to Lambda to publish to SNS const snsPermission = new iam.PolicyStatement({ effect: iam.Effect.ALLOW, actions: ['sns:Publish'], resources: [topic.topicArn] }); handler.addToRolePolicy(snsPermission); // Connect the Lambda function to the SNS topic topic.addSubscription(new sns.Subscription( handler.functionArn, sns.SubscriptionProtocol.LAMBDA )); // Create an event rule to trigger the Lambda function const rule = new lambda.EventSourceMapping(this, 'EventSource', { eventSourceArn: topic.topicArn, target: handler, batchSize: 1, enabled: true }); } } // Define the app const app = new cdk.App(); new MyStack(app, 'MyStack'); app.synth();
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.