amazon-web-services - 使用 CloudFormation 创建和删除 EC2 实例

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

我对 AWS 很陌生,想知道如何使用 CloudFormation 完成以下任务。

我想使用 tomcat 启动 EC2 实例并在其上部署 java 应用程序。这个java应用程序将执行一些操作。操作完成后,我想删除此 CloudFormation 堆栈创建的所有资源。

所有这些事件都应该是自动的。例如,我将创建 CloudFormation 堆栈 JSON 文件。在一天中的特定时间,应该启动一项作业(我不知道在 AWS 中的何处配置此类作业或如何配置)。但我知道通过 Jenkins 我们可以创建一个 CloudFormation 堆栈来创建所有资源。

然后,一段时间后(假设 2 小时),另一个作业应该启动并删除 CloudFormation 创建的所有资源。

这在 AWS 中可行吗?如果是,有关于如何执行此操作的任何提示吗?

最佳答案

只是为了确认一下,您打算做的是按计划创建 EC2 实例,然后在 2 小时后将其关闭。实现这一点的常见方法是使用 Auto-Scaling Group (ASG)ScheduledAction扩大规模并 ScheduledAction缩小规模。

ASG 具有“所需容量”(ASG 中的实例数量)。您可能希望默认值为“0”,在您想要的时间将其更改为“1”,两小时后将其更改回“0”。它将按照您的计划自动启动并随后终止您的 EC2 实例。

他们还使用LaunchConfiguration ,这是将按计划启动的 EC2 实例的模板。

MyASG: 
  Type: AWS::AutoScaling::AutoScalingGroup
  Properties: 
    AvailabilityZones: !GetAZs !Ref "AWS::Region"
    LaunchConfigurationName: !Ref MyLaunchConfiguration
    MaxSize: 1
    MinSize: 0
    DesiredCapacity: 0

ScheduledActionUp: 
  Type: AWS::AutoScaling::ScheduledAction
  Properties:
    AutoScalingGroupName: !Ref MyASG
    DesiredCapacity: 1
    Recurrence: "0 7 * * *"

ScheduledActionDown: 
  Type: AWS::AutoScaling::ScheduledAction
  Properties:
    AutoScalingGroupName: !Ref MyASG
    DesiredCapacity: 0
    Recurrence: "0 9 * * *"

MyLaunchConfiguration:
  Type: AWS::AutoScaling::LaunchConfiguration
  Properties:
    ImageId:  ami-xxxxxxxxx # <-- Specify the AMI ID that you want
    InstanceType: t2.micro # <-- Chaneg the instance size if you want
    KeyName: my-key # <-- Change to the name of an EC2 SSH key that you've added
    UserData: 
      Fn::Base64: !Sub |
        #!/bin/bash
        yum install -y aws-cfn-bootstrap
        # ...
        # ... run some commands to set up the instance, if you need to
        # ...
  Metadata:
    AWS::CloudFormation::Init:
      config:
        files:
          "/etc/something/something.conf":
            mode: 000600
            owner: root
            group: root
            content: !Sub |
              #
              # Add the content of a config file, if you need to
              #

根据您希望实例与之交互的内容,您可能还需要添加 Security Group和/或IAM Instance Profile以及 IAM Role .

如果您使用 Jenkins 部署将要运行的程序,则需要添加一个步骤来烘焙 AMI、构建并推送 docker 镜像,或者采取将应用程序部署到其所在位置所需的任何其他操作将由您的实例使用。

我注意到,在您的问题中,您说您想要删除 CloudFormation 创建的所有资源。通常,当您部署这样的堆栈时,堆栈将保持部署状态。 ASG 将保留在那里,直到您决定删除堆栈,但当您不运行 EC2 实例时,它不会产生任何费用。我想我理解您的意图,因此我给出的建议与此一致。

关于amazon-web-services - 使用 CloudFormation 创建和删除 EC2 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48014833/

相关文章:

amazon-web-services - 使用替换的顶级参数的类型

mysql - 如何在使用 LOAD DATA INFILE 导入大数据文件时保持 MYSQL 性能?

database - 配置 Couchbase Server 4.0 测试版

amazon-web-services - Sid 属性在关键策略中有何用途?

amazon-web-services - 如何删除或更新AWS安全组中的特定规则?

amazon-web-services - AWS ElastiCache for Redis 集群与复制组

python - 如何使用 boto3 在 python 中为 RDS 获取 AWS 区域名称(例如美国东部(弗吉尼亚北部))

amazon-web-services - AWS ALB 终端节点返回 404

node.js - docker-compose & pm2 : container exiting immediately

aws-cloudformation - StringList 类型的 cloudformation SSM 动态引用可以解析为列表吗?