Uploading XML file from Postman to S3 bucket through API Gateway with Cognito Authorizers using Lambda
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
- In the AWS Management Console, click Services then select API Gateway under Application Services.
- Choose Create API.
- Select New API and give the API name as “test1_api”.
- 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.
- Click on Action button and select Create Method
- Select the POST method from the drop down menu
- Select Lambda Function for the integration type.
- Provide the existed lambda function name from lambda.
- 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
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.
- Under newly created API, choose Authorizers.
- Chose Create New Authorizer.
- Provide the name for the Authorizer.
- Select the type of Authorizer as Cognito.
- 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).
- Provide the user pool name in the Cognito User Pool input.
- Provide the “Authorization” as for the Token Source.
- 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
- Click on Actions and select Deploy API from the drop-down list.
- Select [New Stage] for Deployment stage from drop-down list.
- Enter the Stage name as “dev”.
- Click on Deploy button.
- 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:
Create an Amazon S3 Bucket
- Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/.
- Choose Create bucket.
- In the Bucket name field, type a unique name for our new bucket.
- For Region, choose the region where we want the bucket to reside.
- 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
- Open the AWS Lambda console.
- Choose Create a function.
Enter theFunction name as (
Select the run time as Python(3.7).
Select Execution role and choose “use an existing role”. Select existing IAM role.
Click on Create Function as shown below.
Writing Python code in lambda function
def lambda_handler(event, context):
s3 = boto3.client(‘s3’)
s3_response = s3.put_object(Bucket=BUCKET_NAME, Key=file_path, Body=file_data)
except Exception as e:
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.
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
- Go to Headers
- 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
- Click on binary, CHOOSE FILES option will be available
- Choose the path of the XML file(example employee.xml).
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.