【问题标题】:Cannot remote (rdp) into EC2 started from aws lambda using boto3::run_instances无法使用 boto3::run_instances 从 aws lambda 远程 (rdp) 进入 EC2
【发布时间】:2019-07-12 06:39:37
【问题描述】:

当我通过 Web 控制台从特定 AMI 启动 EC2 实例时,它工作得很好,我可以 RDP 进入它没有问题。

但是当我通过 aws lambda 启动另一个(相同的)实例时,我无法 RDP 进入该实例

详情

这是用于启动实例的 lambda

import boto3
REGION = 'ap-southeast-2' 
AMI = 'ami-08e9ad7d527e4e95c'
INSTANCE_TYPE = 't2.small' 
def lambda_handler(event, context):
    EC2 = boto3.client('ec2', region_name=REGION)
    init_script = """<powershell>
powershell "C:\\Users\\Administrator\\Desktop\\ScriptToRunDaily.ps1"
aws ec2 terminate-instances --instance-ids 'curl http://169.254.169.254/latest/meta-data/instance-id'
</powershell>"""
    instance = EC2.run_instances(
        ImageId=AMI,
        InstanceType=INSTANCE_TYPE,
        MinCount=1,
        MaxCount=1, 
        InstanceInitiatedShutdownBehavior='terminate', 
        UserData=init_script 
    )

我可以看到实例在 AWS 控制台中启动。一切看起来都很正常,直到我进入远程,提示说“启动远程会话”需要大约 15 秒并返回

We couldn't connect to the remote PC. Make sure the PC is turned on and connected to the network, and that remote access is enabled.

Error code: 0x204

注意

当我单击尝试通过 AWS 控制台连接到实例时,它允许我下载 RDP 文件,但是,它不会显示“获取密码”选项,因为如果我通过它启动完全相同的 AMI控制台(而不是通过 lambda)

我怀疑我可能需要在启动时将实例与密钥对相关联?

另请注意

在创建这个特定的 AMI 之前,我登录并更改了密码,所以我真的不需要使用 .pem 文件生成一个。

【问题讨论】:

  • 您是否在同一个公共子网中启动?我没有看到您在 lambda 中指定一个
  • 我不这么认为。我应该吗?
  • 尝试传递一个 vpc 和子网,并确保子网是公共的
  • @RodrigoM 我可以从boto docs 看到如何做到这一点。但是,我不知道去哪里或如何获取 'SubnetId': 'string''VpcId': 'string' 的实际值
  • 我尝试从成功(手动启动)的 ec2 中复制值,但没有成功。 "Parameter validation failed:\nUnknown parameter in input: \"VpcId\", must be one of: BlockDeviceMappings, ImageId, InstanceType, Ipv6AddressCount, Ipv6Addresses, KernelId, KeyName, MaxCount, MinCount, Monitoring, Placement, RamdiskId, SecurityGroupIds, SecurityGroups, SubnetId, UserData, AdditionalInfo, ClientToken, DisableApiTermination, DryRun, EbsOptimized, IamInstanceProfile, InstanceInitiatedShutdownBehavior, NetworkInterfaces, PrivateIpAddress, ElasticGpuSpecification等等等等

标签: amazon-web-services amazon-ec2 aws-lambda


【解决方案1】:

原来我需要添加SecurityGroupIds

请注意,它是一个最多包含 5 个值的数组,而不是单个值,因此它被指定为 ['first', 'second', 'etc'] 而不仅仅是 'first'。因此下面['launch-wizard-29'] 周围的方括号

我还指定了一个键。

以下对我有用

import boto3
REGION = 'ap-southeast-2' 
AMI = 'ami-08e9ad7d527e4e95c'
INSTANCE_TYPE = 't2.small' 
def lambda_handler(event, context):
    EC2 = boto3.client('ec2', region_name=REGION)
    init_script = """<powershell>
powershell "C:\\Users\\Administrator\\Desktop\\ScriptToRunDaily.ps1"
aws ec2 terminate-instances --instance-ids 'curl http://169.254.169.254/latest/meta-data/instance-id'
</powershell>"""
    instance = EC2.run_instances(
        ImageId=AMI,
        InstanceType=INSTANCE_TYPE,
        MinCount=1,
        MaxCount=1, 
        InstanceInitiatedShutdownBehavior='terminate', 
        UserData=init_script,
        KeyName='aws', # Name of a key - I used a key (i.e. pem file) that I used for other instances
        SecurityGroupIds=['launch-wizard-29'] # I copied this from another (running) instance
    )


【讨论】:

    猜你喜欢
    • 2023-03-17
    • 2011-08-30
    • 2021-10-23
    • 2016-08-20
    • 2021-02-24
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多