Skip to content

Generated copy of the AWS serverless Java container library's starter app for deploying and running a Spring Boot web app/APIs on the AWS Lambda platform, using AWS API Gateway as a proxy.

Notifications You must be signed in to change notification settings

neiljbrown/aws-serverless-java-container-spring-boot-starter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

aws-serverless-java-container-spring-boot-starter serverless API

This project (including the rest of this README below) is a copy of the AWS Serverless Java Container project’s starter app for running a Spring Boot based app on AWS Lambda, with minor customisations. It was generated from the AWS Serverless Java Container project's Maven archetype using the following command, as documented in the project’s Quick start Spring Boot guide -

$ mvn archetype:generate -DgroupId=com.neiljbrown \
  -DartifactId=aws-serverless-java-container-spring-boot-starter \
  -Dversion=0.0.1-SNAPSHOT \
  -DarchetypeGroupId=com.amazonaws.serverless.archetypes \
  -DarchetypeArtifactId=aws-serverless-springboot2-archetype \
  -DarchetypeVersion=1.3

Post generation some minor updates have been made (e.g. to the project Gradle build script to support packaging & deployment). See the commit logs post initial import for details.

The remainder of this file below, and the majority of the project's code is as it was generated by the above command.

Neil Brown, 03/2019.


The aws-serverless-java-container-spring-boot-starter project, created with aws-serverless-java-container.

The starter project defines a simple /ping resource that can accept GET requests with its tests.

The project folder also includes a sam.yaml file. You can use this SAM file to deploy the project to AWS Lambda and Amazon API Gateway or test in local with SAM Local.

Using Maven, you can create an AWS Lambda-compatible zip file simply by running the maven package command from the projct folder.

$ mvn archetype:generate -DartifactId=aws-serverless-java-container-spring-boot-starter -DarchetypeGroupId=com.amazonaws.serverless.archetypes -DarchetypeArtifactId=aws-serverless-springboot2-archetype -DarchetypeVersion=1.3 -DgroupId=com.neiljbrown -Dversion=0.0.1-SNAPSHOT -Dinteractive=false
$ cd aws-serverless-java-container-spring-boot-starter
$ mvn clean package

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.546 s
[INFO] Finished at: 2018-02-15T08:39:33-08:00
[INFO] Final Memory: XXM/XXXM
[INFO] ------------------------------------------------------------------------

[Update/Addition] - You can also use Gradle to package the Lambda distribution using the following command -

$ gradle build

The generated distribution zip file will be created in the (non-standard for Gradle) 'target' folder configured in sam.yaml for compatibility & interchangeability with Maven.

You can use AWS SAM Local to start your project.

First, install SAM local:

$ npm install -g aws-sam-local

Next, from the project root folder - where the sam.yaml file is located - start the API with the SAM Local CLI.

$ sam local start-api --template sam.yaml

...
Mounting com.amazonaws.serverless.archetypes.StreamLambdaHandler::handleRequest (java8) at http://127.0.0.1:3000/{proxy+} [OPTIONS GET HEAD POST PUT DELETE PATCH]
...

Using a new shell, you can send a test ping request to your API:

$ curl -s http://127.0.0.1:3000/ping | python -m json.tool

{
    "pong": "Hello, World!"
}

You can use the AWS CLI to quickly deploy your application to AWS Lambda and Amazon API Gateway with your SAM template.

You will need an S3 bucket to store the artifacts for deployment. Once you have created the S3 bucket, run the following command from the project's root folder - where the sam.yaml file is located:

$ aws cloudformation package --template-file sam.yaml --output-template-file output-sam.yaml --s3-bucket <YOUR S3 BUCKET NAME>
Uploading to xxxxxxxxxxxxxxxxxxxxxxxxxx  6464692 / 6464692.0  (100.00%)
Successfully packaged artifacts and wrote output template to file output-sam.yaml.
Execute the following command to deploy the packaged template
aws cloudformation deploy --template-file /your/path/output-sam.yaml --stack-name <YOUR STACK NAME>

As the command output suggests, you can now use the cli to deploy the application. Choose a stack name and run the aws cloudformation deploy command from the output of the package command.

$ aws cloudformation deploy --template-file output-sam.yaml --stack-name ServerlessSpringApi --capabilities CAPABILITY_IAM

Once the application is deployed, you can describe the stack to show the API endpoint that was created. The endpoint should be the ServerlessSpringApi key of the Outputs property:

$ aws cloudformation describe-stacks --stack-name ServerlessSpringApi
{
    "Stacks": [
        {
            "StackId": "arn:aws:cloudformation:us-west-2:xxxxxxxx:stack/ServerlessSpringApi/xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx", 
            "Description": "AWS Serverless Spring API - com.amazonaws.serverless.archetypes::aws-serverless-springboot2-archetype", 
            "Tags": [], 
            "Outputs": [
                {
                    "Description": "URL for application",
                    "ExportName": "AwsServerlessJavaContainerSpringBootStarterApi",
                    "OutputKey": "AwsServerlessJavaContainerSpringBootStarterApi",
                    "OutputValue": "https://xxxxxxx.execute-api.us-west-2.amazonaws.com/Prod/ping"
                }
            ], 
            "CreationTime": "2016-12-13T22:59:31.552Z", 
            "Capabilities": [
                "CAPABILITY_IAM"
            ], 
            "StackName": "ServerlessSpringApi", 
            "NotificationARNs": [], 
            "StackStatus": "UPDATE_COMPLETE"
        }
    ]
}

Copy the OutputValue into a browser or use curl to test your first request:

$ curl -s https://xxxxxxx.execute-api.us-west-2.amazonaws.com/Prod/ping | python -m json.tool

{
    "pong": "Hello, World!"
}

About

Generated copy of the AWS serverless Java container library's starter app for deploying and running a Spring Boot web app/APIs on the AWS Lambda platform, using AWS API Gateway as a proxy.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages