1

I know that it's possible to use a CloudFormation template to launch a new EC2 instance in AWS and install any packages using user data.

But is there any way to connect to an existing instance and execute a shell file using the CloudFormation template?

Philip Pittle
  • 10,003
  • 7
  • 47
  • 100

2 Answers2

0

If you want to do this on an existing instance and you are forced to use cloudformation.

You can create a SystemManager Document to run on the instance with cloudformation i.e.

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'
David Webster
  • 1,496
  • 1
  • 11
  • 22
  • Thanks for the suggestion, Don't I have to configure any credentials for the connection establishment? – itsdhandapani Sep 22 '20 at 07:23
  • You would need credentials to run the cloudformation but System Manager is a service that connects to AWS instances that are running the System Manager agent i.e Amazon Linux, Ubuntu and Windows amis this comes preconfigured. The instance will also need System Manager managed instance profile in the instance role. – David Webster Sep 22 '20 at 07:26
  • 1
    Its better then the last answer, but still, how do you execute the command from cloud formation? This will not run anything on the instance, its just creates a document. – Marcin Sep 22 '20 at 07:32
-1

Sadly, this is currently not possible in pure CloudFormatoin. To enable this, you would have to develope a [custom resource][1] in CloudFormation.

The resource would be in the form of a lambda function which would use AWS SDK to run SSM Run Command on your instance, provided that it was configured to work with SSM.

Alternatively, you could use tools such as Paraminko to ssh into the instance from the lambda function in your custom resource.

Marcin
  • 108,294
  • 7
  • 83
  • 138
  • This is not true you can add a user_data script which executes the shell commands – David Webster Sep 22 '20 at 07:06
  • @DavidWebster, Yes I can use user_data script to execute shell commands while launching the instance but In my case, I want to connect to an existing instance and execute a shell script using CF – itsdhandapani Sep 22 '20 at 07:09
  • You could do this theoretically but that makes no sense, it is better to use AWS System Manager to use a RunCommand to execute your command. – David Webster Sep 22 '20 at 07:13
  • @DavidWebster Did you even read the question?. Its about **existing instance**! – Marcin Sep 22 '20 at 07:13
  • 1
    @Marcin You have understood the issue and thanks for the suggestion – itsdhandapani Sep 22 '20 at 07:15
  • @itsdhandapani No problem. I see I got devoted by someone who also probably did not read the question :-) – Marcin Sep 22 '20 at 07:16