Access API gateway endpoint in cloudformation using custom resource

拈花ヽ惹草 提交于 2020-05-31 04:06:11

问题


I want to be able to call a API gateway endpoint from within cloudformation and parse the response from the output and pass in relevant information to one of the other service in the cloudformation.

I have an api endpoint

https://123x123x.execute-api.eu-west-2.amazonaws.com/myendpoint/tenants

with

x-api-key: b8Yk6m63rq8XRnMDKa2PeWE3KvBcU7ZyFIn0Vvrty
Content-Type: application/json

which returns

{
 "tenants": [
        {
            "tenantId": "tenant-1234",
            "AZ": "us-west-2c",
            "tenantUsers": 24,
            "instanceType": "m1.small"
        },
        {
            "tenantId": "tenant-2345",
            "AZ": "us-west-2b",
            "tenantUsers": 32,
            "instanceType": "t2.micro"
        },
        {
            "tenantId": "tenant-3456",
            "AZ": "us-west-2a",
            "tenantUsers": 12
            "instanceType": "m1.large"
        }
    ]}

I want to be able to set the InstanceTypeParameter which needs to be a list ["t2.micro", "m1.small", "m1.large"] retrieved from the above response and passed in as parameter in cloudformation as below.

"Ec2Instance" : {
  "Type" : "AWS::EC2::Instance",
  "Properties" : {
    "InstanceType" : { "Ref" : "InstanceTypeParameter" },
    "ImageId" : "ami-0ff8a91507f77f867"
  }
}

I am assuming the only way to do this would be using a custom resource. Can someone help me develop that (atleast a pseudocode)?


回答1:


You are correct, it must be a custom resource. Below I will provide general steps which can be fallowed to achieve your aim.

  1. Develop a standalone lambda function. Just plain, regular function for now, which is going to call the API, gets its response, parse it prepare result you require based on input parameters you will provide. The aim is to test how such lambda function will work. Its like a blue-print for a custom resource to be developed.

  2. Once you know how the lambda function will work, its time to prepare a custom resource. I recommend creating a new function for that using custom-resource-helper. The helper simplifies a lot development of custom resources. To use it, you will have to prepare a zip deployment package to bundle it with your function handler. Since you know from step 1 exactly how your function should work from step 1, you need to amend it to work in context of the helper. Adding modified the code into def create(event, context) of the helper should be enough. delete(event, context) can be empty as you are not creating any new physical resource in AWS. update(event, context) its up to you want to do with that.

  3. Once you deploy your custom resource lambda, its time to actually create a custom resource in your CFN tempalte. General form is as follows:

MyGetExternalApiResponseResource:
  Type: Custom::CallExternalAPI
  Version: "1.0"
  Properties:
    ServiceToken: <ARN of function from step 2>
    InputParameterToFunction1: <for example, api key>
    InputParameterToFunction2: <for example, url of api to call>
  1. Lots of debugging and troubleshooting. It will almost center not work first time.

  2. Once it works, you can get return values from the custom resource, using either !Ref MyGetExternalApiResponseResource or !GetAtt MyGetExternalApiResponseResource.InstanceTypeParameter. Depends which way you prefare. Second way would be better probably, as the custom resource doesn't create physical resource. Usually !Ref would be used for id of physical resource created, e.g. id of an AMI, id of an instance.

To fully automate it, you would also deploy the code for custom lambda as a CFN template, instead of doing this manually. In this scenario your template would both create a custom resource lambda function, and a custom resource itself using the function.



来源:https://stackoverflow.com/questions/61925210/access-api-gateway-endpoint-in-cloudformation-using-custom-resource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!