【问题标题】:Developing an app to create a stack from a cloudformation template开发应用程序以从 cloudformation 模板创建堆栈
【发布时间】:2018-12-22 04:06:40
【问题描述】:

我是 AWS 新手,目前正在处理简单的任务。

我使用 cloudformation 模板创建了一个免费层级 EC2 实例。现在我的下一个任务是编写一个简单的应用程序,使用相应的 AWS 开发工具包调用 CloudFormation API 以从模板创建堆栈。

这里是cloudformation模板:

{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Ec2 Template",
"Metadata": {
    "Copyright":[
        "Copyright 2017, All rights reserved"
        ],
        "Comments":[
            "Create an EC2"
        ]
},
"Parameters": {
    "KeyName": {
        "Type": "AWS::EC2::KeyPair::KeyName",
        "Description": "Name of an existing EC2 KeyPair to enable access to join ECS instances."
    },
"InstanceTypeParameter":{
    "Type": "String",
    "Default": "t2.micro",
    "AllowedValues": [
    "t2.micro",
    "m1.small",
    "m1.large"
    ],
    "Description": "Enter t2.micro, m1.small, or m1.large. Default is t2.micro."
},
"EcsSecurityGroupLb":{
    "Type": "AWS::EC2::SecurityGroup::Id",
    "Description":"The ECS ELB Security Group."
},
"vpcid":{
    "Type": "AWS::EC2::VPC::Id"
},
"mySubnetIDs": {
    "Description":"Subnet IDs",
    "Type":"AWS::EC2::Subnet::Id"
}
   },
    "Resources":{
        "Ec2Instance":{
            "Type":"AWS::EC2::Instance",
            "Properties":{
                "ImageId": "ami-bf4193c7",
                "KeyName": {
                "Ref": "KeyName"
            },
            "InstanceType":{
                "Ref": "InstanceTypeParameter"
            },

            "NetworkInterfaces":[
              { 
                "AssociatePublicIpAddress":"true",
                "DeviceIndex":"0",
                "SubnetId":{
                    "Ref":"mySubnetIDs"
                },
                "GroupSet":[
                    {
                        "Ref": "EcsSecurityGroupLb"
                    }
                ]
            }
        ],
            "BlockDeviceMappings":[
                {
                    "DeviceName": "/dev/sdc",
                    "VirtualName":"ephemeral0"
                }
            ]
        }
    }
},
"Outputs":{
   "Ec2Instance":{
    "Description": "InstanceId of newly created EC2 instance",
    "Value": {
        "Ref": "Ec2Instance"
        }
    },
    "InstanceIPAddress":{
    "Value":{ "Fn::GetAtt": ["Ec2Instance", "PublicIp"]},
    "Description": "Public IP address of instance"
    }        
  }
}           

我浏览了很多文档,但还没有真正了解如何进行。我想知道这方面是否有任何好的教程。

也在寻找有关步骤的建议。

谢谢!

【问题讨论】:

  • 真的是写应用程序吗?或者您是否愿意使用第三方工具,例如 Ansible?最好不要在不必要的时候重新发明轮子。
  • 编写应用程序是一项要求。任务说明中没有提及其他内容。

标签: amazon-web-services amazon-ec2 aws-sdk amazon-cloudformation


【解决方案1】:

由于您必须(作为任务要求)自己编写应用程序,因此您需要使用可用的one of the AWS SDKs

您选择的 SDK 取决于您最习惯使用的编程语言(或您的任务所需)。

大致而言,您的程序将需要执行以下操作:

  1. 使用 AWS 开发工具包,创建一个使用您的 IAM 用户的 API 密钥的 AWS 会话。
  2. 从本地系统中获取 Cloudformation 模板。
  3. 使用 AWS 开发工具包,调用 Cloudformation 以使用您的模板创建资源堆栈。
  4. (可选)等待堆栈完成并输出堆栈状态报告。

祝你好运!

【讨论】:

    猜你喜欢
    • 2018-11-24
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 2020-02-14
    • 1970-01-01
    • 2018-06-05
    • 2018-08-24
    • 2021-10-12
    相关资源
    最近更新 更多