0

I am creating a CloudFormation where:

  1. An instance is selected
  2. The CloudFormation template has inputs that are to be used in the script
  3. Executes this script into the selected instance

How do I execute a Powershell script from CloudFormation?

3
  • What do you mean by "an instance is selected"? CloudFormation can create new resources and you can specify properties for the new resource, but it cannot perform operations on an existing instance. Commented Feb 11, 2019 at 3:20
  • @JohnRotenstein A dropdown for an existing instance ID is on the CloudFormation template and then the script is executed on that instance Commented Feb 11, 2019 at 3:22
  • Oh! No, that is not possible using CloudFormation. It can create new resources, but cannot modify existing resources. If you wish to run a script on an existing Amazon EC2 instance, consider using AWS Systems Manager Run Command - AWS Systems Manager. Commented Feb 11, 2019 at 3:56

1 Answer 1

1

You can pass a PowerShell script via the User Data. It will be executed on the first startup of the instance.

Here is an example script:

<powershell>
$file = $env:SystemRoot + "\Temp\" + (Get-Date).ToString("MM-dd-yy-hh-mm")
New-Item $file -ItemType file
</powershell>

See: Running Commands on Your Windows Instance at Launch - Amazon Elastic Compute Cloud

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.