Deploy a Lambda function using AWS SAM with CodeBuild Lambda Java
The AWS Serverless Application Model (AWS SAM) is an open-source framework for building serverless applications. For more information,
see the AWS Serverless Application Model repository
Set up your AWS SAM repository
Create an AWS SAM Hello World
project using the AWS SAM CLI.
To create your AWS SAM Project
-
Follow the instructions in the AWS Serverless Application Model Developer Guide for Installing the AWS SAM CLI on your local machine.
-
Run
sam init
and select the following project configuration.Which template source would you like to use?: 1 - AWS Quick Start Templates Choose an AWS Quick Start application template: 1 - Hello World Example Use the most popular runtime and package type? (Python and zip) [y/N]: N Which runtime would you like to use?: 8 - java21 What package type would you like to use?: 1 - Zip Which dependency manager would you like to use?: 1 - gradle Would you like to enable X-Ray tracing on the function(s) in your application? [y/N]: N Would you like to enable monitoring using CloudWatch Application Insights? [y/N]: N Would you like to set Structured Logging in JSON format on your Lambda functions? [y/N]: N Project name [sam-app]: <insert project name>
-
Upload the AWS SAM project folder to a supported source repository. For a list of supported source types, see ProjectSource.
Create a CodeBuild Lambda Java project
Create an AWS CodeBuild Lambda Java project and set up the IAM permissions needed for the build.
To create your CodeBuild Lambda Java project
-
Open the AWS CodeBuild console at https://console.aws.amazon.com/codesuite/codebuild/home
. -
If a CodeBuild information page is displayed, choose Create build project. Otherwise, on the navigation pane, expand Build, choose Build projects, and then choose Create build project.
In Project name, enter a name for this build project. Build project names must be unique across each AWS account. You can also include an optional description of the build project to help other users understand what this project is used for.
-
In Source, select the source repository where your AWS SAM project is located.
-
In Environment:
-
For Compute, select Lambda.
-
For Runtime(s), select Java.
-
For Image, select aws/codebuild/amazonlinux-x86_64-lambda-standard:corretto21.
-
For Service role, leave New service role selected. Make a note of the Role name. This will be required when you update the project’s IAM permissions later in this sample.
-
-
Choose Create build project.
-
Open the IAM console at https://console.aws.amazon.com/iam/
. -
In the navigation pane, choose Roles and select the service role associated with your project. You can find your project role in CodeBuild by selecting your build project, choosing Edit, Environment, and then Service role.
-
Choose the Trust relationships tab, and then choose Edit trust policy.
-
Add the following inline policy to your IAM role. This will be used to deploy your AWS SAM infrastructure later on. For more information, see Adding and removing IAM identity permissions in the IAM User Guide.
{ "Version": "2012-10-17", "Statement": [ { "Sid": "", "Effect": "Allow", "Action": [ "cloudformation:*", "lambda:*", "iam:*", "apigateway:*", "s3:*" ], "Resource": [ "*" ] } ] }
Set up the project buildspec
In order to build, test, and deploy your Lambda function, CodeBuild reads and executes build commands from a buildspec.
To set up your project buildspec
-
In the CodeBuild console, select your build project, then choose Edit and Buildspec.
-
In Buildspec, choose Insert build commands and then Switch to editor.
-
Delete the pre-filled build commands and paste in the following buildspec.
version: 0.2 env: variables: GRADLE_DIR: "HelloWorldFunction" phases: build: commands: - echo "Running unit tests..." - cd $GRADLE_DIR; gradle test; cd .. - echo "Running build..." - sam build --template-file template.yaml - echo "Running deploy..." - sam package --output-template-file packaged.yaml --resolve-s3 --template-file template.yaml - yes | sam deploy
-
Choose Update buildspec.
Deploy your AWS SAM Lambda infrastructure
Use CodeBuild Lambda to automatically deploy your Lambda infrastructure
To deploy your Lambda infrastructure
-
Choose Start build. This will automatically build, test, and deploy your AWS SAM application to AWS Lambda using AWS CloudFormation.
-
Once the build has finished, navigate to the AWS Lambda console and search for your new Lambda function under the AWS SAM project name.
-
Test your Lambda function by selecting API Gateway under the Function overview, then clicking the API endpoint URL. You should see a page open with the message
"message": "hello world"
.
Clean up your infrastructure
To avoid further charges for resources you used during this tutorial, delete the resources created by your AWS SAM template and CodeBuild.
To clean up your infrastructure
-
Navigate to the AWS CloudFormation console and select the
aws-sam-cli-managed-default
. -
In Resources, empty the deployment bucket
SamCliSourceBucket
. -
Delete the
aws-sam-cli-managed-default
stack. -
Delete the AWS CloudFormation stack associated with your AWS SAM project. This stack should have the same name as your AWS SAM project.
-
Navigate to the CloudWatch console and delete the CloudWatch log groups associated with your CodeBuild project.
-
Navigate to the CodeBuild console and delete your CodeBuild project by choosing Delete build project.