Uploading XML file from Postman to S3 bucket through API Gateway with Cognito Authorizers using Lambda

Seeni Lathasree Reddy
5 min readJul 10, 2019

--

When we needed to give our customers the ability to send binary files to our cloud application, I had to find a stable and secure way to upload the files. AWS API Gateway binary support makes it possible to send requests with any content type. API Gateway has caching capability to increase performance.

Create a New REST API in API Gateway

  1. In the AWS Management Console, click Services then select API Gateway under Application Services.
  2. Choose Create API.
  3. Select New API and give the API name as “test1_api”.
  4. Click on Create API as show in below screen shot.

Then create a POST method for that resource and configure it to use a Lambda proxy integration.

  1. Click on Action button and select Create Method
  2. Select the POST method from the drop down menu
  3. Select Lambda Function for the integration type.
  4. Provide the existed lambda function name from lambda.
  5. Choose Save As shown below.

6. When prompted to give Amazon API Gateway permission to invoke our function, choose OK.

Creating an User pool in AWS Console

For creating User pools in cognito follow the below link

https://medium.com/@chandupriya93/life-after-aws-security-identity-compliance-security- identity-compliance-3ebafa0658c1

Create a Cognito User Pool Authorizer

Using Amazon API Gateway to authenticate API calls using JWT tokens returned by Cognito user pools. In the Amazon API Gateway console, create a new Cognito user pool authorizer for our API. Configure it with the details of the user pool that we created in the above api. We can test the configuration in the console by using the auth token presented after we log in via the sign in page of our current website.

  1. Under newly created API, choose Authorizers.
  2. Chose Create New Authorizer.
  3. Provide the name for the Authorizer.
  4. Select the type of Authorizer as Cognito.
  5. In the Cognito User Pool, select the Region based on which region created the Cognito user pool (by default the current region should be selected).
  6. Provide the user pool name in the Cognito User Pool input.
  7. Provide the “Authorization” as for the Token Source.
  8. Click on create. as show in below screen shot.

Cognito User Pool Custom Authorizer is created as shown below

Go back to the Resources where post method is created and click on the method request and select Authorization as cognito user pool name test_auth as we created in above

Deploy our API

  1. Click on Actions and select Deploy API from the drop-down list.
  2. Select [New Stage] for Deployment stage from drop-down list.
  3. Enter the Stage name as “dev”.
  4. Click on Deploy button.
  5. Note the Invoke URL. We will use it in the next section.

6. Note down the Invoke URL — it is the base path of our deployed API. It has the following format: https://{api_id}.execute-api.{region}.amazonaws.com/{stage_name}/.

Create an Amazon S3 Bucket

  1. Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/.
  2. Choose Create bucket.
  3. In the Bucket name field, type a unique name for our new bucket.
  4. For Region, choose the region where we want the bucket to reside.
  5. Click Create.

We have created a bucket in Amazon S3.

Writing Python Code for Storing XML file in S3 bucket in Lambda

Create a Lambda Function in the AWS Console

  1. Open the AWS Lambda console.
  2. Choose Create a function.
  3. Enter the Function name as (procedlambda).
  4. Select the run time as Python(3.7).
  5. Select Execution role and choose “use an existing role”. Select existing IAM role.
  6. Click on Create Function as shown below.

Writing Python code in lambda function

import json
import base64
import boto3

def lambda_handler(event, context):
print(event[‘body-json’])
file_data=event[‘body-json’]
print(“file_data”,file_data)
BUCKET_NAME =’proced’
file_path =’employee.xml’
s3 = boto3.client(‘s3’)
try:
s3_response = s3.put_object(Bucket=BUCKET_NAME, Key=file_path, Body=file_data)
except Exception as e:
raise IOError(e)
return {
‘statusCode’: 200,
‘body’: {
‘file_path’: file_path
}
}

In the above code We given S3 bucket name as proced that already present in the S3 and file path is the name of the file where we store the xml file.

Uploading xml file to S3 Bucket via API using postman and Lambda

Now open postman application.Use the API https://zhsc0dleuj.execute-api.us-east-1.amazonaws.com/dev (Created in the API Gateway) in the Postman endpoint bar and select the method type POST in drop down.

Checking authorization using credentials

  1. Go to Headers
  2. Enter the following key value pairs in Headers

Authorization:”JWT token from user pool user”

3. Enter the content type key value pair in Headers

Content-Type : ”application/xml”

Then click on Request Body tab

  1. Click on binary, CHOOSE FILES option will be available
  2. Choose the path of the XML file(example employee.xml).

Results

Run the API Gateway endpoint url using postman.We get the status code is 200 in the response for the url in post man. Based on status code we assumed that the url is working in correct manner.

Now check in S3 bucket the xml file is Stored like as shown in the below image.

--

--