amazon-web-services - 如何仅将私有 IP 分配给 EC2 实例?
问题描述
下面是在公共子网中创建 EC2 实例的 cloudformation 代码,取自这里:
"EC2Instance":{ "Type": "AWS::EC2::Instance", "Properties":{ "ImageId": "ami-05958d7635caa4d04", "InstanceType": "t2.micro", "SubnetId": { "Ref": "SubnetId"}, "KeyName": { "Ref": "KeyName"}, "SecurityGroupIds": [ { "Ref": "EC2InstanceSecurityGroup"} ], "IamInstanceProfile": { "Ref" : "EC2InstanceProfile"}, "UserData":{ "Fn::Base64": { "Fn::Join": ["", [ "#!/bin/bash\n", "echo ECS_CLUSTER=", { "Ref": "EcsCluster" }, " >> /etc/ecs/ecs.config\n", "groupadd -g 1000 jenkins\n", "useradd -u 1000 -g jenkins jenkins\n", "mkdir -p /ecs/jenkins_home\n", "chown -R jenkins:jenkins /ecs/jenkins_home\n" ] ] } }, "Tags": [ { "Key": "Name", "Value": { "Fn::Join": ["", [ { "Ref": "AWS::StackName"}, "-instance" ] ]} }] } },
默认情况下,公共 IP 被分配给 EC2 实例:
如何让 EC2 实例只分配私有 IP?
解决方案
这很容易。
设置AssociatePublicIpAddress
为false
.
"Ec2Instance" : {
"Type" : "AWS::EC2::Instance",
"Properties" : {
"ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "AMI" ]},
"KeyName" : { "Ref" : "KeyName" },
"NetworkInterfaces": [ {
"AssociatePublicIpAddress": false,
"DeviceIndex": "0",
"GroupSet": [{ "Ref" : "myVPCEC2SecurityGroup" }],
"SubnetId": { "Ref" : "PublicSubnet" }
} ]
}
}
参考: https ://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-instance.html
推荐阅读
- javascript - Content Editable = true,但一次只取一个值
- ios - 如何创建一个字节数组以发送到 BLE 设备并从中获得响应?
- python - 将 3D numpy 数组的切片快速组合为 3D 子数组
- android - 在单个应用程序和 kivy 模拟器中在 kivy 上使用 2 个页面布局
- angular - 在 Angular / Ionic 中发送 HTTPS 请求
- shell - 为什么在没有空文件夹时总是返回找到空文件夹?
- javascript - 使用 XMLHttpRequest 发送数据
- azure - 具有本地访问权限的 Azure 跨区域 VNet 连接
- llvm - LLVM 代码生成器:机器代码表示是否与机器无关?
- redis - Redis 组合键和内存使用命令