amazon-web-services - 使用cloudformation将弹性IP添加到ec2实例

标签 amazon-web-services aws-cloudformation

我可以在哪里从我的 cloudformation 模板附加弹性 IP 吗?

{
"Description" : "Staging single instance",
"Outputs": {
    "InstanceID": {
        "Description": "The WWW instance id",
        "Value": { "Ref": "StageInstance" }
    }
},
"Parameters": {
    "AMI": {
        "Description": "The Amazon Ubuntu AMI",
        "Type": "String",
        "Default": "ami-009110a2bf8d7dd0a"
    },
    "EBSVolumeSize": {
        "Description": "The size of the EBS volume for the transcoder",
        "Type": "String",
        "Default": "20"
    },
    "InstanceType": {
        "AllowedValues": [
            "t2.micro",
            "t2.small",
            "t2.medium",
            "t2.large",
            "c4.large",
            "c4.xlarge",
            "c4.2xlarge",
            "c4.4xlarge",
            "c4.8xlarge",
            "t3.medium"
        ],
        "ConstraintDescription": "must be a valid EC2 instance type",
        "Default": "t2.micro",
        "Description": "EC2 instance type",
        "Type": "String"
    },
    "KeyName": {
        "Description" : "Name of an existing EC2 KeyPair to enable SSH access to NAT instances.",
                    "Type": "AWS::EC2::KeyPair::KeyName",
                    "ConstraintDescription" : "Must be the name of an existing EC2 KeyPair."        }

},
"Resources": {
     "InstanceProfile" : {
         "Type" : "AWS::IAM::InstanceProfile",
                "Properties" : {
                        "Path" : "/",
                                "Roles" : ["Ec2CloudDeploy"]
                                        }
                                            },
    "StageSecurityGroup": {
        "Type": "AWS::EC2::SecurityGroup",
        "Properties": {
            "GroupDescription": "Allow SSH, HTTP, and HTTPS access",
            "SecurityGroupIngress": [
                {
                    "IpProtocol": "tcp",
                    "FromPort": "22",
                    "ToPort": "22",
                    "CidrIp": "0.0.0.0/0"
                },
                {
                    "IpProtocol": "tcp",
                    "FromPort": "80",
                    "ToPort": "80",
                    "CidrIp": "0.0.0.0/0"
                },
                {
                    "IpProtocol": "tcp",
                    "FromPort": "443",
                    "ToPort": "443",
                    "CidrIp": "0.0.0.0/0"
                }
            ]
        }
    },
 "StageInstance": {
        "Type" : "AWS::EC2::Instance",

        "Properties": {
            "SecurityGroupIds": [{"Ref": "StageSecurityGroup"}],
            "KeyName": {"Ref": "KeyName" },
            "ImageId": {"Ref": "AMI"},
            "InstanceType": {"Ref": "InstanceType"},
            "IamInstanceProfile" : {"Ref" : "InstanceProfile"},
            "Tags": [
                {"Key" : "Staging", "Value" : "Staging"}
            ]

 }
  }
 }
}


是否可以添加任何配置来将弹性 IP 附加到该实例将启动的实例。我知道我可以使用 cli 命令附加它,但我想通过我的 cloudformation 模板添加它。

最佳答案

当然。如 AWS::EC2::EIP 中所述:

Specifies an Elastic IP (EIP) address and can, optionally, associate it with an Amazon EC2 instance.

通过设置 InstanceId,您可以将 EIP 关联到 CloudFormation 的 EC2 实例。

对于您的情况,此代码片段应该可以解决问题:

"Resources": {
    ...,
    "ElasticIP": {
        "Type": "AWS::EC2::EIP",
        "Properties": {
            "InstanceId": {"Ref" : "StageInstance"}
        }
    }
}

关于amazon-web-services - 使用cloudformation将弹性IP添加到ec2实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57901148/

相关文章:

amazon-web-services - 如何在AWS Cloudformation中调试 "Resource creation timed out waiting for completion"?

amazon-web-services - 我在 AWS SSL Activate 上使用时出现错误?

python - 在python中使用Hadoop处理大型csv文件

amazon-web-services - 在 Cloud Formation 中为 SQS 创建 VPC 接口(interface)端点

amazon-web-services - 使用 AWS CLI 将现有资源导入 CloudFormation

ubuntu - 连接到托管 Chef 的 Cloudformation 片段

node.js - AWS 用户数据无法在 Windows Server 2019 上运行

java - 如何将S3ObjectInputStream转换为Base64 url​​?

amazon-web-services - 握手后 AWS IoT 关闭 mqtt 连接

amazon-web-services - 我们需要在 AWS 中转义 "Fn::Base64: !Sub"中的任何内容吗?