# Lambda from Container w/ s3 Trigger --- ![Stability: Stable](https://img.shields.io/badge/stability-Stable-success.svg?style=for-the-badge) > **This is a stable example. It should successfully build out of the box** > > This example is built on Construct Libraries marked "Stable" and does not have any infrastructure prerequisites to build. --- This example deploys a lambda function from a container image. It also creates an s3 bucket that triggers the lambda whenever an object is uploaded. This project is intended to be sample code only. Not for use in production. This project will create the following in your AWS cloud environment: - An ECR image. This image gets published to the default ECR repo that is created from `cdk bootstrap` - A Lambda function which is deployed using the above image - A Lambda function which is used internally by CDK to apply configuration notifications on the s3 bucket (read more here https://github.com/aws/aws-cdk/issues/9552#issuecomment-677512510) - A log group in Cloudwatch Logs which contains all logged events from the lambda function's invocations - An s3 bucket with an event notification rule - Roles and policies allowing appropriate access to these resources ## Setup The `cdk.json` file tells the CDK Toolkit how to execute your app. The `go.mod` file defines the module's path and dependency requirements. To download the dependencies locally, run `go mod download`. ## Deploy Run `cdk deploy`. This will deploy / redeploy your Stack to your AWS Account. ## Testing Test the Lambda function by uploading any file into the s3 bucket that got created. Navigate to the Lambda → Monitor → View CloudWatch logs. In the latest log stream you should see a message stating that an object was uploaded. Alternatively, navigate to the Lambda → Test → Create new event. Give the event a name, and paste in the sample data provided under the "Test in console" section https://docs.aws.amazon.com/lambda/latest/dg/with-s3-example.html To test via the AWS CLI, run the following command. `` will be logged at end of the deployment during cdk deploy. Note that this requires AWS CLI v2. ```bash aws lambda invoke --function-name "" \ --payload '{"Records": [{"s3": {"bucket": {"name": "example-bucket"}, "object": {"key": "hello.txt"}}}]}' \ --cli-binary-format raw-in-base64-out \ response.json ``` A `response.json` file will be produced containing the execution log of the invocation. ## Synthesize Cloudformation Template To see the Cloudformation template generated by the CDK, run `cdk synth`, then check the output file in the "cdk.out" directory. # Useful commands - `cdk ls` list all stacks in the app - `cdk synth` emits the synthesized CloudFormation template - `cdk deploy` deploy this stack to your default AWS account/region - `cdk diff` compare deployed stack with current state - `cdk docs` open CDK documentation