使用自定义资源以cloudformation访问API网关端点 [英] Access API gateway endpoint in cloudformation using custom resource

本文介绍了使用自定义资源以cloudformation访问API网关端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够从cloudformation中调用API网关端点,并解析输出的响应,并将相关信息传递给cloudformation中的其他服务之一。

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.

我有一个api端点

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

with

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

返回

{
 "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"
        }
    ]}

我希望能够设置 InstanceTypeParameter ,它需要是列表 [ t2.micro , m1.small, m1.large] 从上述响应中检索出来,并作为参数传递给cloudformation,如下所示。

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)?

推荐答案

您是正确的,它必须是自定义资源。下面,我将提供一些一般性的步骤,可以用来实现您的目标。

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


  1. 开发独立的lambda函数。现在,将要调用API的普通函数是常规的,它会获取响应,根据提供的输入参数解析您所需的结果。目的是测试这种lambda函数如何工作。就像要开发的自定义资源的蓝图

  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.

一旦您知道lambda函数将如何工作,就可以准备自定义资源了。我建议使用 custom-resource-helper 为此功能创建一个新功能。 该帮助程序简化了自定义资源的开发。要使用它,您必须准备一个 zip部署程序包,将其与您的功能处理程序捆绑在一起。由于您从步骤1确切地知道了您的功能从步骤1起应该如何工作,因此您需要对其进行修改以使其在助手的上下文中起作用。将修改后的代码添加到帮助器的 def create(event,context)中就足够了。 delete(event,context)可以为空,因为您没有在AWS中创建任何新的物理资源。 update(event,context)由您决定要执行的操作。

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.

一旦部署了自定义资源lambda,它就可以在CFN tempalte中实际创建自定义资源了。通用格式如下:

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. 很多调试和故障排除。它将几乎第一次无法居中。

  1. Lots of debugging and troubleshooting. It will almost center not work first time.

一旦生效,您可以使用从自定义资源中获取返回值!Ref MyGetExternalApiResponseResource !GetAtt MyGetExternalApiResponseResource.InstanceTypeParameter 。取决于您的宣传方式。第二种方法可能更好,因为自定义资源不会创建物理资源。通常!Ref 将用于创建的物理资源的ID,例如AMI的ID,实例的ID。

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.

完全自动化,您还将手动lambda的代码部署为CFN模板,而不是手动执行。在这种情况下,您的模板将创建一个自定义资源lambda函数,并使用该函数创建一个自定义资源本身。

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.

这篇关于使用自定义资源以cloudformation访问API网关端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆