如何使用CloudFormation模板连接到现有EC2实例并执行Shell文件

问题描述

我知道可以使用CloudFormation模板在AWS中启动 new EC2实例并使用用户数据安装任何软件包。

但是有什么方法可以使用CloudFormation模板连接到现有的 实例并执行Shell文件

解决方法

如果要在现有实例上执行此操作,并且被迫使用cloudformation。

您可以创建一个SystemManager文档以在具有cloudformation的实例上运行,即

document: 
  Type: AWS::SSM::Document
  Properties:
    Content:
      schemaVersion: '2.2'
      description: 'Run a script on Linux instances.'
      parameters:
        commands:
          type: String
          description: "(Required) The commands to run or the path to an existing script
    on the instance."
          default: 'echo Hello World'
      mainSteps:
      - action: aws:runShellScript
        name: runCommands
        inputs:
          timeoutSeconds: '60'
          runCommand:
           - "{{ commands }}"
    DocumentType: Command
    Name: 'CFN_2.2_command_example'
,

遗憾的是,当前在纯CloudFormatoin中无法实现。为此,您必须在CloudFormation中开发一个[自定义资源] [1]。

该资源将采用 lambda函数的形式,如果配置为与SSM配合使用,它将使用AWS开发工具包在您的实例上运行SSM Run Command

或者,您可以使用Paraminko之类的工具从自定义资源中的lambda函数ssh进入实例。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...