AWS Server-less stack creation using AWS CloudFormation

When I first started using AWS Cloud Formation service, I had no idea what to do or how to proceed. There are a lot of blogs which explained the format of cloud formation template and what each property does, but none of them had a sample example which helps beginners to use it to understand and jump right into developing stacks. So, I thought why not write a story that helps fellow developers and here I am. The main focus of this story is to just get started up with cloud formation and have a stack created in half hour or so and not explain each and every Property.

There are a lot of things we can do with Cloud Formation like create lambda, create Dynamo, Deploy API and so on. This story is all about how to set up an Api using gateway, create an endpoint, associate a GET/PUT method and attach an existing lambda with it.

Step 1: Create a Rest API Resource:

Here the object name can be any logical ID

“demoAPIID”: {
 “Type”: “AWS::ApiGateway::RestApi”,
 “Properties”: {
 “Name”: “demoAPI”,
 “Description”: “sample demo API”
 }
 },

Step2: Create a Resource End Point:

Here the object name can be any logical ID.

RestAPiId should contain a reference to the rest api (created above)

“getDemoDetailsResource”: {
 “Type”: “AWS::ApiGateway::Resource”,
 “Properties”: {
 “RestApiId”: {
 “Ref”: “demoAPIID”
 },
 “ParentId”: {
 “Fn::GetAtt”: [
 “demoAPIID”,
 “RootResourceId”
 ]
 },
 “PathPart”: “getDemoDetails”
 }
 }

Step3: Create a Method and attach it to an Endpoint and setup a Lambda:

This is another important step as does the whole mapping of the endpoint to the lambda.

First let’s create a GET Method and have a “MOCK” endpoint.

“getDemoDetailsResourceGETMethod”: {
 “Type”: “AWS::ApiGateway::Method”,
 “Properties”: {
 “RestApiId”: {
 “Ref”: “demoAPIID”
 },
 “ResourceId”: {
 “Ref”: “getDemoDetailsResource”
 },
 “HttpMethod”: “GET”,
 “AuthorizationType”: “NONE”,
 “Integration”: {“Type”:”MOCK”}
 }
 }
Mock EndPoint

The whole lambda integration goes inside the property “Integration”.

The following snippet attaches the lambda to the method. Wait a sec, before you deploy and test on browser.

“Integration”: {
 “Type”: “AWS”,
 “IntegrationHttpMethod”: “POST”,
 “Uri”: {
 “Fn::Join”: [
 “”,
 [
 “arn:aws:apigateway:”,
 {
 “Ref”: “AWS::Region”
 },
 “:lambda:path/2015–03–31/functions/”,
 “arn:aws:lambda:us-east-2:456214169279:function:test”,
 “/invocations”
 ]
 ]
 },
 “IntegrationResponses”: [
 {
 “StatusCode”: 200
 }
 ]
 }
Lambda Integrated — Look at the Integration Response and Method Responses

Now lets setup the final part — the method response, which goes right after Integration property

“MethodResponses”: [
 {
 “StatusCode”: 200,
 “ResponseModels”: {
 “application/json”: “Empty”
 }
 }
 ]

Now look at the Method Response, It has a status 200 and an empty model. This just passes the output from lambda to the browser.

To test this. Just click Test button on the left or deploy the API which can be done using the Deploy API button Action Dropdown .

Finally, the link to the entire code is: https://gist.github.com/AmareshB/c672defde305aeeb17d3501eb4b2e225

PS: I am a beginner at AWS too. It’s an ocean and it’s really fun to learn.

Let me know if there’s any mistake or anything else in the comments.

Happy Coding!!