Create api-gateway lambda integration using aws-cli - amazon-web-services

I need to create an api gateway using aws client. I successfully create and integrate with my aws-lambda function using web console. But I am confused with aws-client. These are the steps I followed.
Create api gateway and integrate with my sample lambda function using web console.
Deploy created api and export as json file.
Create new api gateway using exported json file using aws-cli. Command like this.
aws apigateway import-rest-api --body file://tmpfile.json --region us-east-1;
But it created only resources & methods.
For integrate api method with my lambda function, I execute command like this
aws apigateway put-integration --rest-api-id 42ku123id8u3a --resource-id core-api-dev --http-method DELETE --type AWS --integration-http-method POST --uri 'arn:aws:lambda:us-east-1:my-lambda-function-arn' --region us-east-1
But it produces error message like this
An error occurred (NotFoundException) when calling the PutIntegration operation: Invalid Resource identifier specified
Is it possible to integrate api gateway method with existing lambda function using aws client? What is Resource identifier?

you can run aws apigateway get-resources to get the resource-id
aws apigateway get-resources --rest-api-id 42ku123id8u3a --region us-east-1
It will return a JSon like
{
"items": [
{
"path": "/resource/xxx",
"resourceMethods": {
"POST": {}
},
"id": "_yourresourceid_",
"pathPart": "xxx",
"parentId": "ai5b02"
}
]
}
you can take the id from this JSon and use it on your command for aws apigateway put-integration

Ideally you should export as JSON in step 2 'with integration extensions'. In the console there are 3 options for export type, and the middle one will include the integrations and authorizers in the export. Then when you import you'll have the integrations already.

Related

Api Gateway change Stage name

How could I change Stage name in AWS API Gateway? I haven't found rename function in console and neither in CLI.
For example creating new stage with CLI:
aws apigateway create-deployment --rest-api-id ${RESTAPIID} --stage-name ''
or
aws apigateway create-stage --rest-api-id abcde123456 --stage-name ThisIsMyStageName --deployment-id abdcde
My best guess is to use "update-stage" function but don't know how to do this.
The API Gateway's stage name is a ressource ID. This mean you cannot edit it.
However, you can easily create a new stage and delete the old one

AWS API GATEWAY import-documentation-parts

I am new in Api Gateway, and I am trying to dynamically update Api Gateway in my pipeline with this command.
aws apigateway import-documentation-parts --no-fail-on-warnings --rest-api-id $AWS_API_GATEWAY_REST_API_ID --mode overwrite --cli-binary-format raw-in-base64-out --body file://docs/openapi/openapi.yaml > importesdocs.json
It returns all the ids of documentation parts, but I can't see any change in my Api Gateway actually.
Then I am trying to perform new action with those ids like.
aws apigateway update-documentation-part --rest-api-id $AWS_API_GATEWAY_REST_API_ID --documentation-part-id $DOCUMENTATION_PART_ID
This haven't worked out as well. Is there any suggestion about how I can reach my goal?

Is it possible to change name of resources of API in AWS API Gateway?

I am new to AWS API Gateways. I created an api named : test-api1. Also, I created a resource named test-resource1. Now I want to change the name of this resource. I couldn't find any option to rename resources.
Is this possible or do I need to create new resource?
The answer to this depends on how you deploy your API. If you use an OpenAPI spec, then you can rename your resource there. If you used a CloudFormation template (or a AWS CDK stack) then you can rename your resource there.
If you merely clicked-together your API in the AWS Management Console (Web UI), then there isn't a way in that Web UI to change the resource's name.
You have to revert to using the AWS CLI for that.
There is the apigateway update-resource AWS CLI command to update the name of an API Gateway API resource.
https://docs.aws.amazon.com/cli/latest/reference/apigateway/update-resource.html
(search for "To rename a resource (pathPart) in an API"):
aws apigateway update-resource --rest-api-id <api-id> --resource-id <resource-id> --patch-operations op=replace,path=/pathPart,value=<new-name>

aws apigateway import-rest-api returns "Invalid base64" error

I am trying to export and than import AWS Gateway API, following the instructions in
https://aws.amazon.com/premiumsupport/knowledge-center/api-gateway-migrate-accounts-regions/.
Export is working:
aws apigateway get-export --parameters extensions='apigateway' --rest-api-id MY_REST_API_ID --stage-name Prod --export-type swagger my-api-apigateway.json
Identical files for my API are generated with --parameters extensions='apigateway'
and --parameters extensions='integrations'.
But when I try to do the import from the exported file:
aws apigateway import-rest-api --fail-on-warnings --body file://%cd%/my-api-gateway.json
, I am always getting "Invalid base64: " error. Like this:
Invalid base64: "{
"swagger" : "2.0",
"info" : {
"version" : "1.0",
"title" : "my-stack-name"
},
"host" : "MY_REST_API_ID.execute-api.eu-central-1.amazonaws.com",
"basePath" : "/Prod",
...
No documentation and no examples on google say that body should be Base64.
The same JSON seems to work when I import it via UI (Actions -> Import API).
I've also tried to use --cli-input-json:
my-api-apigateway-cli-json.json file (according to aws apigateway import-rest-api --generate-cli-skeleton):
{
"failOnWarnings": true,
"parameters": {
"endpointConfigurationTypes": "REGIONAL"
},
"body": {... JSON FROM EXPORT ...}
}
Import command:
aws apigateway import-rest-api --cli-input-json file://./my-api-apigateway-cli-json.json
, but it says
Parameter validation failed:
Invalid type for parameter body, value: {'swagger': '2.0', ...
So, the questions are:
Should we encode the json as base64?
Why is this error and behaviour not documented (or if yes, then where?)?
How to successfully execute import from cli?
Useful links
None of the links say that body response should be Base64
https://docs.aws.amazon.com/apigateway/api-reference/link-relation/restapi-import/ -- here body is clearly documented as // raw byte array representing the api definition
When importing rest-api into AWS API Gateway using the AWS CLI its not mandatory to encode the json as base64.
I suspect you are using AWS CLI v2 and this issue you are facing, I believe it's as a result of changes introduced in AWS CLI version 2. i.e
AWS CLI version 2 now passes all binary input and binary output
parameters as base64-encoded strings by default
Resolution:
You will need add --cli-binary-format raw-in-base64-out so that it tells AWS CLI v2 to revert to the AWS CLI v1 behavior:
aws apigateway import-rest-api --cli-binary-format raw-in-base64-out --body file://my-api-apigateway.json
Using fileb:// instead of file:// worked for me. For example,
aws apigateway import-rest-api --body fileb://my-api.json

“Lambda function ARN must be in same account” while `aws apigateway put-integration`

I am trying to follow this otherwise excellent post to deploy a Java-based AWS lambda app. I'm scripting it out as instructed in that post. I receive the following error when calling aws apigateway put-integration:
An error occurred (BadRequestException) when calling the
PutIntegration operation: Lambda function ARN must be in same account
I'm trying to script the creation of an API gateway to a lambda function. Here's the full deploy.sh script. I have on my local machine the requisite AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_ACCOUNT_ID environment variables.
I don't know enough to understand what that error is telling me. AFAIK, the access secret and id and account ID are all from the same IAM user.
My AWS CLI Script
aws apigateway put-integration --region ap-south-1 --rest-api-id xxttj6inea --resource-id bgncc0 --http-method GET --type AWS --integration-http-method POST --uri arn:aws:apigateway:ap-south-1:lambda:path/2019-09-20/functions/arn:arn:aws:lambda:ap-south-1:***********:function:Hello/invocations
Thank you very much in advance for any ideas/help.
This error can happen because of two reasons:
Your AWS CLI settings are incorrect. I'll implore you to check the AWS CLI configuration file, and run test API calls for the same to verify if everything is set up appropriately.
Your Lambda function belongs in one account, and your REST API in API GW belongs in another account.