Kshitij Kohli
Kshitij Kohli

Reputation: 4393

How to run user-data commands in Cloudformation on EC2 as some other user?

What I have noticed is whenever user-data commands are executed while executing a Cloudformation template to spin up an EC2, they are run via the root user. How can I change this behaviour so that the commands are run as ec2-user?

I tried doing a

sudo su ec2-user

to tackle this, but that apparently opens up another shell on the machine so doesn't really solve this use case.

Upvotes: 1

Views: 1009

Answers (1)

kichik
kichik

Reputation: 34744

sudo has the -u parameter that lets you choose the user. So you can use:

sudo -u ec2-user echo hello from ec2-user

Upvotes: 1

Related Questions