How to Use AWS CodeStar to Manage Lambda Java Project from Source to Test Locally
How do you debug your AWS Lambda functions? Some of the developers put the AWS services’ endpoint ARN(Amazon Resource Names) into unit test code to test locally before deploying into AWS. Is there any AWS service to help the whole source code development life cycle for the application development team? Let’s find out which AWS services we can use to manage source code and debug locally.
We will take a look at the following AWS services to simulate the code development cycle:
- AWS Lambda
- AWS Toolkit (Prerequisite: Eclipse IDE or Visual Studio)
- AWS CodeStar (through AWS CodePipeline to associate with CodeCommit, CodeBuild, CloudFormation, and CloudWatch)
- AWS SAM Local (Prerequisites: Python, Docker )
AWS Lambda is a fully managed compute service that runs your code in response to events generated by custom code or from various AWS services such as Amazon S3, DynamoDB, Kinesis, Amazon SNS, and Amazon Cognito. AWS Lambda supports Java, Node.js, C#, Python and Go code. We will use Java as the example. The price of Lambda is the same as the other services that you pay only for what you use. On the free tier, you have 1M free requests per month ($0.20 per 1m requests thereafter) and 400,000 GB-seconds of compute time per month ($0.00001667 for every GB-seconds used thereafter.)
The AWS Toolkit is an open source IDE plug-in for Eclipse or Visual Studio that makes it easier for developers to develop, debug, and deploy Java applications using Amazon Web Services. We will take a look at it on the Eclipse IDE. The AWS Toolkit for Eclipse provides support for authoring code for AWS Lambda.
The new project that created by AWS CodeStar includes an AWS CodePipeline (a continuous integration and continuous delivery service) to connect the following tools during the project development cycle:
- Source: You can choose AWS CodeCommit or GitHub as your source repository. AWS CodeCommit is a fully-managed source control service that makes it easy for companies to host secure and highly scalable private Git repositories. CodeCommit eliminates the need to operate your own source control system or worry about scaling its infrastructure. You can use CodeCommit to securely store anything from source code to binaries, and it works seamlessly with your existing Git tools. The cost of CodeCommit is free for first 5 active users with unlimited repositories, 50 GB-month of storage and 10,000 Git requests/month. Then $1 per month per active user with unlimited repositories, 10 GB-month of storage per active user and 2,000 Git requests/month per active user.
- Build: The project uses AWS CodeBuild which is a fully managed build service to compile source codes, run tests, and produce software packages that are ready to deploy. You can use the Jenkins plugin for AWS CodeBuild to integrate AWS CodeBuild with your Jenkins build jobs. The calculation of the cost of CodeBuild is little tricky: you always have the Free Tier 100 build minutes of build.general1.small per month. If you exceed the 100 build minutes, then your charge will equal to (total build minutes – free tier minutes)* Unit price per build minute.
- Test: AWS SAM Local to test and debug locally on Eclipse IDE for Lambda Java. You can search Lambda tester under NPM for local test and debug of others supported programming language.
- Deploy: AWS CloudFormation. We mentioned CloudFormation in the article on AWS VPC.
- Monitoring: AWS CloudWatch. We will discuss this service in a separated article.
Every customer has one free active CodePipeline each month and $1 per month per extra active CodePipeline. The cost of AWS CodeStar is the same as CloudFormation. There is no additional charge for using AWS CodeStar. You only pay for the AWS resources after you run the applications.
AWS SAM Local
AWS SAM Local provides a way to debug and test locally on the Lambda functions and AWS API gateway in Java. You need to install Docker and Python in your local before installing SAM CLI. Please follow this link to install Docker and SAM CLI. After you done the installation, configure AWS SAM Local Executable’s path in Eclipse by going to Preferences under AWS SAM Local of AWS Toolkit. The Debug/Run as AWS SAM Local’s setup of CodeStar project will look like this:
The following step-by-step is the best way to create the project from AWS CodeStar to code/debug on Eclipse locally:
- Install AWS Toolkit on Eclipse IDE.
- Download Eclipse IDE for Java EE developers. If you don’t have Java 8 SDK in your local, please go to Oracle to download one.
- Go to Help menu on Eclipse IDE then select either Install New Software… (location: https://aws.amazon.com/eclipse) or Eclipse Marketplace…(search: AWS Toolkit)
- Install AWS SAM Local.
- Create SAM Project from AWS CodeStar.
- Login AWS Console. Go to AWS CodeStar. Then Create Project by selecting Java Spring template. Enter Project name.
- Select AWS CodeCommit for source repository.
- Select Eclipse to edit code.
- Import AWS CodeStar Project to Eclipse.
- On AWS CodeStar Project Select: Select region you created CodeStar Project
- Configure Git Credential by going to AWS console >>IAM>>Users>>Under User’s Security Credentials>>HTTPS Git credentials for AWS CodeCommit>>Generate username and password
- Import the Git credential csv file in Eclipse
- Code and test the project on Eclipse.
- Configure AWS SAM Local. Under Preferences… to configure SAM executable path: the path of aws-sam-cli.
- Go to Debug As or Run As AWS Local. Then Debug or Run configurations…, select the project you created (e.g. mysamproject) and template (e.g. template.yml). You can run both SAM test cases: Lambda function or API gateway. Select function identifier. The generate event (e.g. S3 event) with .json file name.
- It’s ready to code and debug locally.
Please review the detail instructions on my YouTube channel: