如何阻止Ansible每次执行重复创建AWS API Gateway?
Hey there, let's sort out this duplicate API Gateway issue you're facing. I've run into this exact problem before—the root cause is that the aws_api_gateway module can't identify your existing API instance by default, so it treats every playbook run as a request to create a new resource.
Your Current Code
Ansible Playbook
- name: deploy API Gateway tags: deploy aws_api_gateway: state: present region: "{{ aws_region }}" swagger_file: swagger.yml stage: test deploy_desc: test deployment register: api_result - debug: var: api_result - name: test api gateway endpoint uri: url: https://{{ api_result.api_id }}.execute-api.{{ aws_region }}.amazonaws.com/test return_content: yes register: webpage - debug: var: webpage
Swagger File (swagger.yml)
--- swagger: "2.0" info: version: "2018-05-10T07:51:38Z" title: "MyAPI" description: "REST API" host: "rijjh41w9e.execute-api.ap-south-1.amazonaws.com" basePath: "/test" schemes: - "https" paths: /: get: produces: - "application/json" responses: 200: description: "200 response" schema: $ref: "#/definitions/Empty" x-amazon-apigateway-integration: uri: "arn:aws:apigateway:ap-south-1:lambda:path/2015-03-31/functions/arn:aws:lambda:ap-south-1:174336093897:function:MyLambda/invocations" responses: default: statusCode: "200" passthroughBehavior: "when_no_match" httpMethod: "POST" contentHandling: "CONVERT_TO_TEXT" type: "aws" definitions: Empty: type: "object" title: "Empty Schema"
Solutions to Stop Duplicates
We need to give the aws_api_gateway module a unique identifier to match against existing APIs. Here are two reliable ways to do this:
1. Add the name Parameter to Your Playbook
The aws_api_gateway module uses the name parameter to look for existing APIs. If it finds a match, it will update that API instead of creating a new one. I recommend using the same name as your Swagger info.title for consistency:
- name: deploy API Gateway tags: deploy aws_api_gateway: state: present region: "{{ aws_region }}" name: "MyAPI" # Match this to your Swagger title or use any unique name swagger_file: swagger.yml stage: test deploy_desc: test deployment register: api_result
2. Remove the Hardcoded host from Your Swagger File (Optional but Recommended)
Your Swagger file has a fixed host value, which ties it to a specific API domain. When you create a new API, AWS generates a new domain, causing conflicts. Removing the host field lets AWS handle the domain automatically, making it easier for the module to match the correct API:
Modified Swagger snippet:
--- swagger: "2.0" info: version: "2018-05-10T07:51:38Z" title: "MyAPI" description: "REST API" # Delete the host line here basePath: "/test" schemes: - "https" # Rest of your Swagger content stays the same
3. (Alternative) Specify the api_id Directly
If you already know the ID of your existing API (from the host value in your Swagger: rijjh41w9e), you can hardcode it in the playbook. This tells the module to target that exact API every time:
- name: deploy API Gateway tags: deploy aws_api_gateway: state: present region: "{{ aws_region }}" api_id: "rijjh41w9e" # Your existing API's ID swagger_file: swagger.yml stage: test deploy_desc: test deployment register: api_result
How to Verify It Works
After making these changes:
- The first run will create the API if it doesn't exist.
- Subsequent runs will either update the existing API (if you changed the Swagger/configuration) or do nothing (if everything matches).
- Check the
api_resultdebug output: if it's an update,changedwill betruebut theapi_idwill stay the same. If no changes are needed,changedwill befalse.
内容的提问来源于stack exchange,提问作者Mohd Meesam

