|
| 1 | +#set($resourceName = $artifactId) |
| 2 | +#macro(replaceChar $originalName, $char) |
| 3 | + #if($originalName.contains($char)) |
| 4 | + #set($tokens = $originalName.split($char)) |
| 5 | + #set($newResourceName = "") |
| 6 | + #foreach($token in $tokens) |
| 7 | + #set($newResourceName = $newResourceName + $token.substring(0,1).toUpperCase() + $token.substring(1).toLowerCase()) |
| 8 | + #end |
| 9 | + ${newResourceName} |
| 10 | + #else |
| 11 | + #set($newResourceName = $originalName.substring(0,1).toUpperCase() + $originalName.substring(1)) |
| 12 | + ${newResourceName} |
| 13 | + #end |
| 14 | +#end |
| 15 | +#set($resourceName = "#replaceChar($resourceName, '-')") |
| 16 | +#set($resourceName = "#replaceChar($resourceName, '.')") |
| 17 | +#set($resourceName = $resourceName.replaceAll("\n", "").trim()) |
| 18 | +# ${artifactId} serverless API |
| 19 | +The ${artifactId} project, created with [`aws-serverless-java-container`](https://github.com/awslabs/aws-serverless-java-container). |
| 20 | + |
| 21 | +The starter project defines a simple `/ping` resource that can accept `GET` requests with its tests. |
| 22 | + |
| 23 | +The project folder also includes a `sam.yaml` file. You can use this [SAM](https://github.com/awslabs/serverless-application-model) file to deploy the project to AWS Lambda and Amazon API Gateway or test in local with [SAM Local](https://github.com/awslabs/aws-sam-local). |
| 24 | + |
| 25 | +## Building the project |
| 26 | +Using [Maven](https://maven.apache.org/), you can create an AWS Lambda-compatible jar file simply by running the maven package command from the projct folder. |
| 27 | + |
| 28 | +```bash |
| 29 | +$ mvn archetype:generate -DartifactId=my-spring-api -DarchetypeGroupId=com.amazonaws.serverless.archetypes -DarchetypeArtifactId=aws-serverless-spring-archetype -DarchetypeVersion=1.0-SNAPSHOT -DgroupId=com.sapessi.spring -Dversion=0.1 -Dinteractive=false |
| 30 | +$ cd my-spring-api |
| 31 | +$ mvn clean package |
| 32 | + |
| 33 | +[INFO] ------------------------------------------------------------------------ |
| 34 | +[INFO] BUILD SUCCESS |
| 35 | +[INFO] ------------------------------------------------------------------------ |
| 36 | +[INFO] Total time: 6.546 s |
| 37 | +[INFO] Finished at: 2018-02-15T08:39:33-08:00 |
| 38 | +[INFO] Final Memory: XXM/XXXM |
| 39 | +[INFO] ------------------------------------------------------------------------ |
| 40 | +``` |
| 41 | + |
| 42 | +## Testing locally with SAM local |
| 43 | +You can use [AWS SAM Local](https://github.com/awslabs/aws-sam-local) to start your project. |
| 44 | + |
| 45 | +First, install SAM local: |
| 46 | + |
| 47 | +```bash |
| 48 | +$ npm install -g aws-sam-local |
| 49 | +``` |
| 50 | + |
| 51 | +Next, from the project root folder - where the `sam.yaml` file is located - start the API with the SAM Local CLI. |
| 52 | + |
| 53 | +```bash |
| 54 | +$ sam local start-api --template sam.yaml |
| 55 | + |
| 56 | +... |
| 57 | +Mounting ${groupId}.StreamLambdaHandler::handleRequest (java8) at http://127.0.0.1:3000/{proxy+} [OPTIONS GET HEAD POST PUT DELETE PATCH] |
| 58 | +... |
| 59 | +``` |
| 60 | + |
| 61 | +Using a new shell, you can send a test ping request to your API: |
| 62 | + |
| 63 | +```bash |
| 64 | +$ curl -s http://127.0.0.1:3000/ping | python -m json.tool |
| 65 | + |
| 66 | +{ |
| 67 | + "pong": "Hello, World!" |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +## Deploying to AWS |
| 72 | +You can use the [AWS CLI](https://aws.amazon.com/cli/) to quickly deploy your application to AWS Lambda and Amazon API Gateway with your SAM template. |
| 73 | + |
| 74 | +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: |
| 75 | + |
| 76 | +``` |
| 77 | +$ aws cloudformation package --template-file sam.yaml --output-template-file output-sam.yaml --s3-bucket <YOUR S3 BUCKET NAME> |
| 78 | +Uploading to xxxxxxxxxxxxxxxxxxxxxxxxxx 6464692 / 6464692.0 (100.00%) |
| 79 | +Successfully packaged artifacts and wrote output template to file output-sam.yaml. |
| 80 | +Execute the following command to deploy the packaged template |
| 81 | +aws cloudformation deploy --template-file /your/path/output-sam.yaml --stack-name <YOUR STACK NAME> |
| 82 | +``` |
| 83 | + |
| 84 | +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. |
| 85 | + |
| 86 | +``` |
| 87 | +$ aws cloudformation deploy --template-file output-sam.yaml --stack-name ServerlessSpringApi --capabilities CAPABILITY_IAM |
| 88 | +``` |
| 89 | + |
| 90 | +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: |
| 91 | + |
| 92 | +``` |
| 93 | +$ aws cloudformation describe-stacks --stack-name ServerlessSpringApi |
| 94 | +{ |
| 95 | + "Stacks": [ |
| 96 | + { |
| 97 | + "StackId": "arn:aws:cloudformation:us-west-2:xxxxxxxx:stack/ServerlessSpringApi/xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx", |
| 98 | + "Description": "AWS Serverless Spring API - ${groupId}::${artifactId}", |
| 99 | + "Tags": [], |
| 100 | + "Outputs": [ |
| 101 | + { |
| 102 | + "Description": "URL for application", |
| 103 | + "ExportName": "${resourceName}Api", |
| 104 | + "OutputKey": "${resourceName}Api", |
| 105 | + "OutputValue": "https://xxxxxxx.execute-api.us-west-2.amazonaws.com/Prod/ping" |
| 106 | + } |
| 107 | + ], |
| 108 | + "CreationTime": "2016-12-13T22:59:31.552Z", |
| 109 | + "Capabilities": [ |
| 110 | + "CAPABILITY_IAM" |
| 111 | + ], |
| 112 | + "StackName": "ServerlessSpringApi", |
| 113 | + "NotificationARNs": [], |
| 114 | + "StackStatus": "UPDATE_COMPLETE" |
| 115 | + } |
| 116 | + ] |
| 117 | +} |
| 118 | +
|
| 119 | +``` |
| 120 | + |
| 121 | +Copy the `OutputValue` into a browser or use curl to test your first request: |
| 122 | + |
| 123 | +```bash |
| 124 | +$ curl -s https://xxxxxxx.execute-api.us-west-2.amazonaws.com/Prod/ping | python -m json.tool |
| 125 | + |
| 126 | +{ |
| 127 | + "pong": "Hello, World!" |
| 128 | +} |
| 129 | +``` |
0 commit comments