how can I place a key in a AWS RHEL instance with "user data" ?

how can I place a key in a AWS RHEL instacne with "user data"

is it possible to create a "here" script and have it execute on AWS instance "user data"?

like this

user data with a bash script in it#!/usr/bin/env bash
mkdir -p /home/ec2-user/.ssh
cat <<EOF >  /home/ec2-user/.ssh/docker-master-key.pem
-----BEGIN RSA PRIVATE KEY-----
...
-----END RSA PRIVATE KEY-----
EOF

I want to be able to pass a key to an instance upon boot, I have used just this above and it does not seem to do anything at all , I can not find any errors , how can I find out logging from this user data ?

thanks
LVL 1
NAMEWITHELD12Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anthony GarciaDevops StaffCommented:
You should be able to see the output of the user data from the aws console. Right click on the instance -> Instance Settings -> Get System Log.

system_log.jpg
This should show you all of the output from boot time including the stdout of user data scripts.

Something like this should work in the user data. I use something similar.
Don't forget the permissions at the end and adding the -v flag in for bash will make it verbose.

#!/bin/bash -v
cat <<EOF >  /home/ec2-user/.ssh/docker-master-key.pem
-----BEGIN RSA PRIVATE KEY-----
Private key goes here.
-----END RSA PRIVATE KEY-----
EOF
chmod 0400 /home/ec2-user/.ssh/docker-master-key.pem

Open in new window



If this doesn't work how is it not working? Is the file not being generated/showing up in the directory you expect it to?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NAMEWITHELD12Author Commented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.