從 AWS Lambda 函數創建 CloudFormation 堆棧,傳遞 API Gateway 參數 (Create CloudFormation stack from AWS Lambda function, passing API Gateway parameters)


問題描述

從 AWS Lambda 函數創建 CloudFormation 堆棧,傳遞 API Gateway 參數 (Create CloudFormation stack from AWS Lambda function, passing API Gateway parameters)

我無法在 Lambda 函數中獲取參數。如果我在 lambda 中提到參數值,它可以正常工作。當我從 Lambda 函數中刪除參數值並嘗試從 API 網關或測試 lambda 時,它會處理默認參數值。請幫助我的 Lambda 函數是:

import boto3
import time
import json

datetime = time.strftime("%Y%m%d%H%M%S")
stackname = 'myec2'
client = boto3.client('cloudformation')
response = client.create_stack(
  StackName= (stackname+ '‑' + datetime),
  TemplateURL='https://testnaeem.s3.amazonaws.com/ec2tags.yaml',
  Parameters=[
  {
    "ParameterKey": "MyInstanceName",
    "ParameterValue": " "
  },
  {
    "ParameterKey": "MyInstanceType",
    "ParameterValue": " "
  }
]
)


def lambda_handler(event, context):
            return(response)

我的 CloudFormation 模板是:

‑‑‑
Parameters:
  MyInstanceType:
    Description: Instance type description
    Type: String
  MyInstanceName:
    Description: Instance type description
    Type: String  

Resources:
  MyInstance:
    Type: AWS::EC2::Instance
    Properties:
      AvailabilityZone: us‑east‑1a
      ImageId:  ami‑047a51fa27710816e
      InstanceType: !Ref MyInstanceType
      KeyName: miankeyp
      Tags:
        ‑ Key : Name
          Value : !Ref MyInstanceName
        ‑ Key : app 
          Value : demo

請幫助 Lambda 函數需要進行哪些更改。

我的測試值是:

{
  "MyInstanceName": "demott",
  "MyInstanceType": "t2.micro"
}

參考解法

方法 1:

I modified the code of your lambda function. Please check comments in the code for clarification:

import boto3
import time
import json

datetime = time.strftime("%Y%m%d%H%M%S")
stackname = 'myec2'
client = boto3.client('cloudformation')

def lambda_handler(event, context):

    print(event) # to check what your even actually is
    # it will be printed out in CloudWatch Logs for your
    # function

    # you have to check what the event actually looks like
    # and adjust event['MyInstanceName'] and event['MyInstanceType']
    # in the following code

    response = client.create_stack(
      StackName= (stackname+ '‑' + datetime),
      TemplateURL='https://testnaeem.s3.amazonaws.com/ec2tags.yaml',
      Parameters=[
      {
        "ParameterKey": "MyInstanceName",
        "ParameterValue": event['MyInstanceName']
      },
      {
        "ParameterKey": "MyInstanceType",
        "ParameterValue": event['MyInstanceType']
      }
    ]
    )

    return(response)

By the way, such function and API gateway can spin up a lot of ec2 instances very quickly. So that you are aware of this.

(by NaeemMarcin)

參考文件

  1. Create CloudFormation stack from AWS Lambda function, passing API Gateway parameters (CC BY‑SA 2.5/3.0/4.0)

#API #serverless #gateway #amazon-cloudformation #aws-lambda






相關問題

UPS Api - php, làm cách nào để truy cập? (UPS Api - php, how do I get around?)

Google api 地圖和主幹錯誤 (Google api maps and backbone error)

如何將數據從控制器傳遞到 go lang 中的表單? (How can I pass data from controller to form in go lang?)

從 ASP.net 使用 PHP 中的 POST 數據抓取數據 (Scraping data with POST data in PHP from ASP.net)

檢測/指紋手機品牌和型號的替代方法? (Alternative way to detect/fingerprint phone make and model?)

POST:get_responses API 未提供所需數據 (POST : get_responses API not giving required data)

通過 API 檢測 Shopware 版本 (Detect Shopware Version through API)

我的 module.exports 返回為 [object Object] (My module.exports return as [object Object])

API 憑證應該存儲在 laravel 中的哪個位置? (Where API credentials should be stored in laravel?)

Uppy "PUT" XHR(multipart/form-data) 請求結果清空 Laravel $request 數組 (Uppy "PUT" XHR(multipart/form-data) request results to empty Laravel $request array)

JObject 不包含“user_name”的定義,也沒有可訪問的擴展方法“user_name” (JObject does not contain a definition for 'user_name' and no accessible extension method 'user_name')

PUT API 中的 Django Rest Framework 解析錯誤 (Django Rest Framework parse error in PUT API)







留言討論