【问题标题】:Creating an AWS EC2 instance through python boto3 script on Lambda function通过 Lambda 函数上的 python boto3 脚本创建 AWS EC2 实例
【发布时间】:2020-01-28 02:07:35
【问题描述】:

以下是我在自定义 VPC 上启动实例的代码,但我看到以下错误。在查找问题时需要您的帮助:


import json
import boto3

def lambda_handler(event, context):
     session_client=boto3.client(service_name='ec2')
     response = session_client.run_instances(
     #SecurityGroupIds=['sg-0ffb80bb93205a004'],
     #SubnetId='subnet-0bb30254459fd4714',
     MaxCount=1,
     MinCount=1,
     NetworkInterfaces=[
        {
            'NetworkInterfaceId':'eni-0846a5c0c47e98b3c',
            'Description': 'Network Interface Description',
            'DeviceIndex': 123,
            'SubnetId': 'subnet-0bb30254459fd4714'
        },
    ],
     LaunchTemplate={
     'LaunchTemplateId': 'lt-0d3c7dd295796ad6b',
     'Version': '1'
      })

Error: Network interfaces and an instance-level subnet ID may not be specified on the same request",
  "errorType": "ClientError",

谢谢, 巴普雷迪巴图

【问题讨论】:

    标签: python aws-lambda boto3


    【解决方案1】:

    通过boto3创建EC2实例时可以传递两个subnetid参数

    1) 请求中根级别的 SubnetId:

    SubnetId (字符串)-- [EC2-VPC] 要启动 实例成。如果指定网络接口,则必须指定 作为网络接口一部分的任何子网。

    由于您传递的是 NetworkInterfaces 参数,因此不会传递此 SubnetId。

    2) SubnetId 作为 NetworkInterfaces 参数的一部分

    SubnetId (字符串)- 与网络关联的子网 ID 界面。仅在启动时创建网络接口时适用 一个实例。

    因为您使用的是已经存在的网络接口。您也不应该在 NetworkInterfaces 中传递 SubnetId 参数。

    参考文档:EC2 run instances - Boto3 documentation

    【讨论】:

      猜你喜欢
      • 2020-06-19
      • 2018-04-07
      • 2021-02-24
      • 2018-04-18
      • 2015-11-20
      • 2019-06-26
      • 2020-01-22
      • 2020-11-12
      • 2017-10-10
      相关资源
      最近更新 更多