[英]What's the correct attribute to handle 'NatGatewayID'?
I'm having problems retrieving the metadata of my AWS NATGateway resources.我在检索我的 AWS NATGateway 资源的元数据时遇到问题。 I can't seem to find the proper attribute to retrieve the ID.
我似乎找不到合适的属性来检索 ID。
Tried all sorts of attribute like NAT.id and I'm still checking the documentations here [1] [2] [3] to hopefully fix the issue.尝试了各种属性,例如 NAT.id,我仍在检查此处的文档 [1] [2] [3] 以希望解决问题。
[1] https://boto3.amazonaws.com/v1/documentation/api/latest/guide/migration.html [1] https://boto3.amazonaws.com/v1/documentation/api/latest/guide/migration.html
[2] https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.describe_nat_gateways [2] https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.describe_nat_gateways
[3] https://aws.amazon.com/premiumsupport/knowledge-center/start-stop-lambda-cloudwatch/ [3] https://aws.amazon.com/premiumsupport/knowledge-center/start-stop-lambda-cloudwatch/
import boto3
# Region your instances are in, e.g. 'us-east-1'
region = 'ap-southeast-1'
#instantiate
client = boto3.client('ec2',region)
ids = []
def lambda_handler(event, context):
#lists all the metadata of NAT resources having a TagKey:Schedule
#Value:OfficeHours
NATs = client.describe_nat_gateways(
Filter=[
{
'Name': 'tag:Schedule',
'Values': [
'OfficeHours',
],
},
],
)
for NAT in NATs:
print('deleted NAT gateways: ' + NAT.NatGatewayId)
# ids.append(NAT.NatGatewayId)
# client.delete_nat_gateway(NatGatewayId=ids)
Once I retrieve the metadata:NatGatewayID, I should be able to delete these resources via lambda.一旦我检索到元数据:NatGatewayID,我应该能够通过 lambda 删除这些资源。
From the boto docs in the question:从问题中的boto docs:
Response Syntax
{
'NatGateways': [
{
'CreateTime': datetime(2015, 1, 1),
'DeleteTime': datetime(2015, 1, 1),
'FailureCode': 'string',
'FailureMessage': 'string',
'NatGatewayAddresses': [
{
'AllocationId': 'string',
'NetworkInterfaceId': 'string',
'PrivateIp': 'string',
'PublicIp': 'string'
},
],
'NatGatewayId': 'string',
'ProvisionedBandwidth': {
'ProvisionTime': datetime(2015, 1, 1),
'Provisioned': 'string',
'RequestTime': datetime(2015, 1, 1),
'Requested': 'string',
'Status': 'string'
},
'State': 'pending'|'failed'|'available'|'deleting'|'deleted',
'SubnetId': 'string',
'VpcId': 'string',
'Tags': [
{
'Key': 'string',
'Value': 'string'
},
]
},
],
'NextToken': 'string'
}
The response is dict
containing a list of NatGateways
.响应是包含
NatGateways
列表的dict
。 As the response is a dict
, the response's properties are not accessed using object.property
notation;由于响应是
dict
,因此无法使用object.property
表示法访问响应的属性; rather it is object['property']
.而是
object['property']
。
This loop should work:这个循环应该工作:
for NAT in NATs['NatGateways']:
print('deleted NAT gateways: ' + NAT['NatGatewayId'])
...
After a bit of tweaking, I was able to fix the issue. Here's the code:
import boto3
# Region your instances are in, e.g. 'us-east-1'
region = 'ap-southeast-1'
#instantiate
client = boto3.client('ec2',region)
ids = []
def lambda_handler(event, context):
NATs = client.describe_nat_gateways(
Filter=[
{
'Name': 'tag:Schedule',
'Values': [
'OfficeHours',
],
},
],
)
for NAT in NATs['NatGateways']:
print('deleted NAT gateways: ' + NAT['NatGatewayId'])
# ids.append(NAT['NatGatewayId'])
# client.delete_nat_gateway(NatGatewayId=ids)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.