In this lab you will experiment the Amazon API Gateway, and build an integration to AWS Systems Manager’s Parameter Store.
As result, these are the features that you are going to be exposed here:
You need:
We are going to create an API that will consume services from AWS Systems Manager (SSM), using the Parameter Store feature.
Your API will consume the configuration for a system, stored on the Parameter Store. We are considering that all parameters for a certain system will have the form /systems/<systemName>/config
, where <systemName>
is, obvioulsy, the name of the system to which we want to retrieve the configuration.
So, the idea for this API is to retrieve all the configuration for a system that you specify in the query string. The purpose is to design an API that works similarly to what’s provided here:
REQUEST:
https://<api>/getconfig?system=nameOfSystem
RESPONSE:
{
data : {
<configKey1> : <configKey1Value> ,
<configKey2> : <configKey2Value> ,
...
}
}
In this lab, if you are sharing a single AWS Account and region with other users, we recommend you to include your initials as a prefix on the name of the resources. This is shown as <prefix>
in the instructions. You can disregard that if you are alone in an AWS Account and region.
Parameter Store
.Create parameter
.Name
, input what is shown below (replace <prefix>
as previously instructed)/systems/<prefix>system01/config
Description
to provide an informative description about the purpose of this parameter.Tier
, select Standard
. We are willing to run under free tier.Type
is String
for our case.Data Type
is text
.Value
, input the following JSON, as an example:{
"url" : "https://www.amazon.com"
}
Create parameter
.Your API will need to have the proper permissions to access the required services - in this case, Systems Manager. To accomplish this, we will create a role that will give to our API the permissions to consume the parameters from SSM/Parameter Store.
Roles
.Create role
.AWS Service
, then choose API Gateway
, and then click on the button Next: Permissions
.Next:Tags
. At this moment we will not define tags, but this is a very handy resource from a governance perspective.Next: Review
.<prefix>SystemConfigAPI
, and for description input some clarifying description for the role.Create Role
.Click on the role name. You will be taken to the role summary page similar to the one below.
Add inline policy
. The window to create the policy will show up.Service
, select Systems Manager
.Actions
, choose GetParameter
.Resources
, click on Add ARN
.
systems/*/config
. This means that you will be able to retrieve the configuration for all systems. Suppose we are retrieving the config for a system named “ticketing”, then the parameter will be systems/ticketing/config.Add
.Review Policy
.Create Policy
.See that the policy was added to your role. As this role will be used to define the permissions for our API, take note of the role ARN.
Explore the role configuration, to get more awareness about the role structure.
Visit the API Gateway home page.
Click on the Create the API
button
REST API
New API
<prefix>SystemConfigTracker
This API tracks the configuration of all systems under our organization
Regional
.Create API
. You will be taken to a page where you will be able to configure the API.Click on the drop-down button Actions
, and click on Create Resource
. A page titled New Child Resource will show up.
Resource Name
, input getconfig
Create Resource
Creating the HTTP method
/getconfig
resource that you just createdActions
, and click on Create Method
. A drop-down list will appear below /getconfigGET
on that listOn the page entitled /getconfig - GET - Setup
Integration type
, select AWS Service.AWS Region
, select the same region you are working at. Tip: You can check it by looking at the URL on your browserAWS Service
, select Simple Systems Management (SSM)
.HTTP method
, select POST
. We are converting a GET request to a POST request, because the Systems Manager request for GetParameter is a POST.Action Type
, select Use path override
, and then for Path override (optional), input a forward slash (like this: / )Execution Role
, input the IAM role that you created in the previous taskWe will now configure the API Gateway integration with SSM.
Click on Integration Request
Scroll down to HTTP Headers
Click on Add header
, and let’s configure the SSM method that we want to invoke
X-Amz-Target
Mapped from
, input 'AmazonSSM.GetParameter'
. Don’t forget of including the single quotes.Click on Add header
again, and let’s configure the content type expected by the SSM API
Content-Type
Mapped from
, input 'application/x-amz-json-1.1'
.Scroll down to Mapping Templates
. We will configure a mapping template for a testing purpose. A little later we will get back here and configure a transformation by getting data from the query string.
Mapping Templates
Request body passthrough
, select When there are no templates defined (recommended)
Add mapping template
Content-Type
, input application/json
, and then click on the icon to confirm. A text-box will appear so you can input the mapping template.{
"Name" : "/systems/<prefix>system01/config"
}
Save
Scroll up, and click on Method Execution
, to get back to the integration configuration. You will see a vertical bar labeled as Test
at the left
Testing the API
Test
. You will be taken to the API Gateway test page{
"Parameter": {
"ARN": "arn:aws:ssm:<region>:<account>:parameter/systems/<prefix>system01/config",
"DataType": "text",
"LastModifiedDate": 1607124001.465,
"Name": "/systems/<prefix>system01/config",
"Type": "String",
"Value": "{\n \"url\" : \"https://www.amazon.com\"\n}",
"Version": 1
}
}
<- Method Execution
to get back to the previous page.Fine-tunning the integration
Let us improve our integration, by (1) getting the server name from the query string, then inserting it inside the request that is submitted to Systems Manager, and finally (2) let’s provide a clean response for the requestor.
Including the query string value for the key “system” into the integration request
Integration Request
{
"Name" : "/systems/$input.params('system')/config"
}
Save
.<prefix>system01
with the name that you choose.
Now, let’s clean the response for the requestor
Method Execution
to get back to the /getconfig - GET - Method Execution
configuration page.Method response status
.#set($inputRoot=$input.path('$'))
#if ($inputRoot.Parameter.Value && $inputRoot.Parameter.Value!="")
{
"data" : $input.path('$').Parameter.Value
}
#end
Save
.Method Execution
link. Test the API as you did before.{
"data": {
"url": "https://www.amazon.com"
}
}
The API that you just created is not available externally. To make it callable by users and other systems - the clients of the API - you need to deploy it. To deploy an API, you create an API deployment and associate it with a stage. Each stage is a snapshot of the API and is made available for client apps to call. You can read more about it here.
Actions
, and select Deploy API
.prod
stage.
Deploy
. You will be going to be redirected to a page where you have the configuration for the stage. At the top of the page you will see the URL for your API deployment.
We will use CURL to test the API.
<prefix>
with the one that you choose when you created the SSM parameter for your systemcurl https://<api stage deployment URL>/getconfig?system=<prefix>system01
Test your API with a non-existent parameter, both on the console and using CURL. Check the response code, and the response body. Do you think that this is properly configured? Do you think that it’s simple to configure a better response for the API clients?
The API is not secure. For deploying an API in production, and exposing it publicly, you should consider implementing the proper access and throttling controls.
You have finished this lab.
Now, you can delete all the resources from your account (unless guided differently by the presenter):