如何在Boto3中为AWS EC2实例设置标签 [英] How to set tags for AWS EC2 instance in boto3

查看:321
本文介绍了如何在Boto3中为AWS EC2实例设置标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Boto3的新手,并且想创建一个VPC,子网和一些ec2实例.基本体系结构有一个VPC,在2个不同的可用区域(us-east-1a和b)​​中有2个子网,并应用允许SSHping的安全组.

I am new to Boto3, and wanted to create a VPC, subnets, and some ec2 instances. The basic architecture is having a VPC, 2 subnets within 2 different availability zones (us-east-1a and b), and applying a security group which allows SSH and ping.

我的问题是如何为每种资源指定其他选项. Python SDK(与Javadoc的工作方式不同)未显示所需的参数和示例选项,因此感到困惑.

My problem is how to specify additional options for each resources. The Python SDK (unlike how Javadoc works) doesn't show the required arguments and example options, so I'm confused.

如何为资源指定tags? (例如ec2实例).我需要设置nameowner

How can I specify tags for resources? (e.g. ec2 instance). I need to set name, owner, etc.

instances2 = ec2.create_instances(ImageId='ami-095575c1a372d21db', InstanceType='t2.micro', MaxCount=1, MinCount=1, NetworkInterfaces=[{'SubnetId': subnet2.id, 'DeviceIndex': 0, 'AssociatePublicIpAddress': True, 'Groups': [sec_group.group_id]}])
instances2[0].wait_until_running()
print(instances1[0].id)

推荐答案

您需要将'ResourceType'设置为'instance'TagSpecifications参数:

You need the TagSpecifications argument with 'ResourceType' set to 'instance':

TagSpecifications=[
    {
      'ResourceType': 'instance',
      'Tags': [
        {
          'Key': 'name',
          'Value': 'foobar'
        },
        {
          'Key': 'owner',
          'Value': 'me'
        },
      ]
    },
  ],

它在文档中,但您确实需要知道要查找的内容...

It is in the docs but you do need to know what you're looking for...

这篇关于如何在Boto3中为AWS EC2实例设置标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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