You have a few ways to would be suitable.
- Take in parameters for username and password for CloudFormation. Use the NoEcho parameter. You can then do a
!Ref AdminPassword
in the output but this is really not a good idea.
Template
Parameters:
AdminPassword:
Description: Admin Password
Type: String
NoEcho: true
MinLength: 8
MaxLength: 32
ConstraintDescription: Must be at least 8 chars long
You also need to add this to userdata using !Sub to string replace
<powershell>
cmd.exe /c net user /add admin ${AdminPassword}
cmd.exe /c net localgroup administrators admin /add
cmd.exe /c NET localgroup "Remote Desktop Users" admin /ADD
<powershell>
aws ec2 --region ap-southeast-2 get-password-data --priv-launch-key secret.pem --instance-id i-123123124
- SSM parameters are a good option too