Example of integration of api gateway and java lambda using cloud-formation. The REST API deployed is defined using OpenAPI 3.0 and returns JSON from the single do method.
This project has been updated to use
- OpenAPI 3.0 with 200, 302, 400, 500 responses
- properly mapped errors
- an S3 redirect example
- a binary response example (see the '/image` path in cloudformation.yaml
- full json api
This project is all about the cloudformation.yaml script and the lambda handler. Have a look at those files which have been generously commented to understand more of how the api gateway to java lambda integration happens.
You can deploy this application to your own AWS account by following the steps below. It's not a bad idea just to check that stuff is working. Then you can copy the project, refactor package names, artifact and groupId names and customize to your liking.
- Maven installed
- an AWS account with key and secret key access pair that has privileges to create resources
- a
<server>entry in~/.m2/settings.xmlwith your aws key and secret key in the username and password fields (you can encrypt the password field usingmvn -ep).
./deploy.sh -Dserver.id=<SERVERID> -Dapplication=<YOUR_APP_NAME>If you are behind a proxy you can set -Dproxy.host=<PROXY> -Dproxy.port=<PORT> in the above command also.
The call to deploy.sh will do the following:
- build the project jar artifact
- create a bucket for artifacts (for lambda) for the application if doesn't exist (a unique name built from
applicationand your AWS account ID will be used) - deploy the built maven artifact versioned and timestamped to the artifact bucket
- create a CloudFormation stack comprising
- lambda
- lambda execution role
- api gateway
- api gateway stage
- single api user authenticated by
x-api-keyheader - api usage plan for the single api user
- one GET path that maps a String response from the lambda handler to
text/plaincontent - permission for the api gateway to call the lambda (including the Test interface in the AWS Console)
- deploy the stage (not performed automatically by a CloudFormation update)
Suppose your application name was myapp.
In the AWS Console go to API Gateway - myapp - Stages - api - Invoke URL and copy the url displayed there.
Also in the AWS Console go to API Gateway - API Keys - myapp-user - API Key - Show and copy it also.
URL= ...
X_API_KEY= ...
curl -H "x-api-key: $X_API_KEY" "$URL/api/do?name=fred"prints out (JSON)
"Hello fred"
You can also test the api in the AWS Console at API Gateway - myapp - Resources - /do - GET - Test. Enter a value for name and click the Test button and you will see the response in the right of the frame.
To remove the whole stack go to the AWS Console at CloudFormation - Stacks - myapp - Actions - Delete Stack.
There is one more item you can remove that is created by deploy.sh and that is the artifact bucket in S3 which is called app-<ACCOUNT_ID>-artifacts.
- a big hint to speed your development is work on the cloudformation stuff first (paths, parameters, integrations) then do the work in the java handler. The reason for this is that as you add dependencies for the handler (java libraries) the jar deployed to lambda will grow and your test cycle will become longer because of the delays in uploading the jar to AWS.