amazon-web-services - 如何将 NAT 实例置于自动缩放中?

标签 amazon-web-services amazon-ec2 nat aws-vpc

我想创建一个容错网站并尝试创建一个自动缩放组。使用 NAT 实例而不是 NAT 网关。我遇到了以下问题。

当 NAT 实例 [由于某种原因] 终止时。自动缩放组将启动相应的 NAT 镜像,但它不会禁用源/目标检查。这必须手动完成,因此,连接到 nat 实例的私有(private)子网将具有状态消息“黑洞”。除非手动更改源目标检查,否则私有(private)子网甚至不会显示新的 NAT 实例。

有没有人能解决这个问题?

最佳答案

您可以使用 AWS CLI 修改网络接口(interface)的 sourceDestCheck 属性。您可以从实例的用户数据启动它。其他方法是自定义 python 程序甚至 PowerShell(包括在下面)。

aws ec2 modify-instance-attribute --instance-id i-1234567890abcdef0 --source-dest-check "{\"Value\": true}"

或者通过更改网络接口(interface)。

aws ec2 modify-network-interface-attribute --network-interface-id eni-686ea200 --no-source-dest-check

这个 Stack Overflow 问题涵盖了在 Python 中做同样的事情。

Disable Source/Destination Check AWS Python Boto

本文档展示了如何在 PowerShell 中执行此操作。

Edit-EC2NetworkInterfaceAttribute Cmdlet

我的首选方法是 Python 或 PowerShell。这些方法是获取所需参数(instanceId 或 networkId)的最简单方法。

[编辑:示例程序]

以下两个示例都需要在实例上安装凭证或 IAM 角色。这是修改源/目标标志所必需的。

这是一个示例 shell 脚本。

EC2_INSTANCE_ID="`wget -q -O - http://169.254.169.254/latest/meta-data/instance-id`"
EC2_AVAIL_ZONE="`wget -q -O - http://169.254.169.254/latest/meta-data/placement/availability-zone`"
EC2_REGION="`echo \"$EC2_AVAIL_ZONE\" | sed -e 's:\([0-9][0-9]*\)[a-z]*\$:\\1:'`"
echo "Region:" $EC2_REGION

aws ec2 modify-instance-attribute --instance-id $EC2_INSTANCE_ID --source-dest-check "{\"Value\": false}" --region $EC2_REGION

rc=$?; if [[ $rc != 0 ]]; then echo "Failure:" $rc; exit $rc; fi

echo "Success"

这是一个使用 boto3 的 Python 2 程序示例(将区域更改为您的区域):

#!/usr/bin/python
import boto3
import requests
import sys

# install boto3
# sudo pip install boto3

# Disable stack trace on failure
sys.tracebacklimit = 0

# Specify the URL for the instance metadata
url = 'http://169.254.169.254/latest/meta-data/instance-id'

# Specify the region where our instance is at
region = 'us-west-2'

# Make a request to get the contents of the URL
r = requests.get(url)

if r.ok != True:
    print "Error: Failed to get instance-id from metadata:", r.reason
    print "Status Code:", r.status_code
    sys.exit(1)

# Get the instance ID from the return response
instance_id = r.text

print "Instance ID:", instance_id

if instance_id[0] != 'i':
    print "Error: Does not look like a valid instance ID: ", instance_id
    sys.exit(1)

client = boto3.client('ec2', region_name=region)

r = client.modify_instance_attribute(InstanceId=instance_id, SourceDestCheck={'Value': False})

code = r['ResponseMetadata']['HTTPStatusCode']

if code != 200:
    print "Error: Cannot change SourceDestCheck: ", code
    sys.exit(1)

print "Success: SourceDestCheck disabled"

关于amazon-web-services - 如何将 NAT 实例置于自动缩放中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48363115/

相关文章:

python - 使用 Elastic Beanstalk 上的 Supervisor 在后台运行 Huey 任务队列

javascript - Javascript 是否只能通过 NAT 穿透?

python - 如何在Python中通过NAT确定出站端口

amazon-web-services - 如何通过 AWS SES 向多个收件人发送电子邮件

ubuntu -/usr/src 在 Amazon Ubuntu EC2 中包含什么?

amazon-web-services - 如何使用 CloudFormation 组织 ECS CodePipeline

amazon-ec2 - 如何从cloudformation中的快照创建EC2实例?

Linux 对我自己的 IP 地址进行 NAT

amazon-web-services - 无法将 lambda 与 cloudwatch 事件触发器链接

amazon-web-services - AWS 实验室 VPC 无法创建