I have created AWS Lambda python3.6 function to launch new windows ec2 instance. Additionally i have added powershell script as user data while launching new instance. But instance has been launched successfully. But user data is not executed.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import boto3
AMI = os.environ["AMI"]
INSTANCE_TYPE = os.environ["INSTANCE_TYPE"]
KEY_NAME = os.environ["KEY_NAME"]
SUBNET_ID = os.environ["SUBNET_ID"]
REGION = os.environ["REGION"]
ec2 = boto3.client("ec2", region_name=REGION)
def lambda_handler(event, context):
init_script = '''
<powershell>
$USERNAME="Latchu"
$PASSWORD="eM2An@ydxk"
net user /add $USERNAME $PASSWORD
</powershell>'''
instance = ec2.run_instances(
ImageId=AMI,
InstanceType=INSTANCE_TYPE,
KeyName=KEY_NAME,
SubnetId=SUBNET_ID,
MaxCount=1,
MinCount=1,
InstanceInitiatedShutdownBehavior="terminate",
UserData=init_script,
)
After lauched EC2 instance logon to windows server and i had a look. But it seems, user not created. Am i missing anything in my code?
C:\ProgramData\Amazon\EC2Launch\log\agent.log. Try removing the spaces at the start of the lines, especially the first link with<powershell>. Let us know how that goes!