This project contains source code and supporting files for a serverless app built using AWS SAM. Given a Twitter username, this app shows back a random tweet liked by that user.
It includes the following files and folders.
functions/RandomLikesFunction/src/main
- Code for the application's Lambda function written in Java.functions/RandomLikesFunction/src/test
- Unit tests for the application code.functions/RandomLikesFunction/pom.xml
- Maven file defining all the required dependencies and configured to a build an uberjar for deployment.functions/template.yaml
- SAM template that defines the application's AWS resources.public
- Front end code in HTML, some CSS and vanilla JS. You may deploy the same on Netlify.
You can deploy the app on AWS using SAM CLI. To use the SAM CLI for this app, you need the following tools.
- SAM CLI - Install the SAM CLI
- Java8 - Install the Java SE Development Kit 8
- Maven - Install Maven
To build and deploy your application for the first time, run the following in your shell:
sam build
sam deploy --guided
The first command will build the source of your application. The second command will package and deploy your application to AWS, with a series of prompts:
- Stack Name: The name of the stack to deploy to CloudFormation. This should be unique to your account and region, and a good starting point would be something matching your project name.
- AWS Region: The AWS region you want to deploy your app to.
- Confirm changes before deploy: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes.
- Allow SAM CLI IAM role creation: Many AWS SAM templates, including this example, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, these are scoped down to minimum required permissions. To deploy an AWS CloudFormation stack which creates or modified IAM roles, the
CAPABILITY_IAM
value forcapabilities
must be provided. If permission isn't provided through this prompt, to deploy this example you must explicitly pass--capabilities CAPABILITY_IAM
to thesam deploy
command. - Save arguments to samconfig.toml: If set to yes, your choices will be saved to a configuration file inside the project, so that in the future you can just re-run
sam deploy
without parameters to deploy changes to your application.
You will find the API Gateway Endpoint URL in the output values displayed after deployment. Update the outputted URL in public/index.js
file to invoke the same from UI so as to view the tweet in the browser.
Build your application with the sam build
command.
functions$ sam build
The SAM CLI installs dependencies defined in functions/RandomLikesFunction/pom.xml
, creates a deployment package, and saves it in the functions/.aws-sam/build
folder.
Tests are defined in the functions/RandomLikesFunction/src/test
folder in this project.
functions$ cd RandomLikesFunction
RandomLikesFunction$ mvn test
To delete the sample application that you created, use the AWS CLI. Assuming you used "random-likes" as stack name, you can run the following:
aws cloudformation delete-stack --stack-name random-likes
See the AWS SAM developer guide for an introduction to SAM specification, the SAM CLI, and serverless application concepts.