I am trying to create an API Gateway using the AWS CLI. I’ve previously set up and integrated it with my Lambda function using the web interface, but I’m unsure about the CLI process. Here are the steps I’ve taken so far:
-
Created an API Gateway and linked it to a sample Lambda function using the web console.
-
Deployed the API and exported it as a JSON file.
-
I attempted to create a new API Gateway with the JSON file using the following command:
aws apigateway import-rest-api --body file://tmpfile.json --region us-east-1;
However, this only created the resources and methods.
-
To connect the API method to my Lambda function, I executed:
aws apigateway put-integration --rest-api-id abc123xyz --resource-id api-resource-id --http-method DELETE --type AWS --integration-http-method POST --uri 'arn:aws:lambda:us-east-1:your-lambda-function-arn' --region us-east-1
But I received an error message:
An error occurred (NotFoundException) during the PutIntegration operation: Invalid Resource identifier specified.
Can anyone help me understand how to correctly integrate an API Gateway method with an existing Lambda function using the AWS CLI? What does ‘Resource identifier’ refer to in this context?
When integrating API Gateway with a Lambda function via the AWS CLI, ensuring the correct identifiers and setup is crucial. Here's a step-by-step approach to resolve the issues you encountered:
- Ensure Correct Resource-ID: The error "Invalid Resource identifier specified" suggests that the resource-id provided might not be accurate. Use the list-resources command to retrieve valid resource identifiers.
aws apigateway get-resources --rest-api-id abc123xyz --region us-east-1
<p>Find the ID that corresponds to the resource you wish to integrate.</p>
- Check Lambda Permissions: Ensure that your Lambda function's execution role has the correct permissions and the API Gateway can invoke it. Attach AWSLambdaRole and AWSAPIGatewayServiceRole policies to the role.
aws lambda add-permission --function-name your-lambda-function-name --statement-id some-unique-id --action lambda:InvokeFunction --principal apigateway.amazonaws.com --region us-east-1
- Correct URI Format: Double-check the URI in the put-integration command. It should precisely follow this format:
'arn:aws:apigateway::lambda:path/2015-03-31/functions//invocations'
- Update Integration: Once you have the correct resource-id and validated permissions, try re-running the put-integration command with the accurate details.
aws apigateway put-integration --rest-api-id abc123xyz --resource-id --http-method DELETE --type AWS_PROXY --integration-http-method POST --uri 'arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/your-lambda-function-arn/invocations' --region us-east-1
If these steps are followed accurately, your integration should work without any errors. In case of further issues, ensure the CLI configuration matches the AWS credentials required and the Lambda's region matches the API Gateway region.