To create user credential in EC2 instance from remote using Boto3 or Python

+1 vote

Dear all,

I have tried to creating user credentials in AWS EC2 windows instance using Boto3. I just searched for a lot of things and tried. Below one is what I find to execute the AWS Powershell script to create a user name and password.

import boto3
ssm_client = boto3.client('ssm', region_name="us-west-2") # use region code in which you are working
response = ssm_client.send_command(
             InstanceIds=[
                "i-03########" # use instance id on which you want to execute, even multiple is allowd
                     ],
             DocumentName="AWS-RunPowerShellScript",
             Parameters={
                'commands':[
                     'ipconfig'
                       ]
                   },
             })
command_id = response['Command']['CommandId']
output = ssm_client.get_command_invocation(
      CommandId=command_id,
      InstanceId='i-03######',
    )

print(output)

Can anyone tell me? What should I do to create a user name and password in the AWS Windows Ec2 instance?

Oct 7, 2020 in AWS by Lakshminarayanan
• 1,370 points

edited Oct 7, 2020 by MD 2,489 views

1 answer to this question.

+1 vote

Hi@Lakshminarayanan,

You can create a user from your PowerShell. In your script, you can use the below two commands. I think these commands will work.

>> $password = Read-Host -AsSecureString
>> New-LocalUser "redhat" -Password $Password -FullName "Redhat User" -Description "User for Redhat system"
answered Oct 7, 2020 by MD
• 95,460 points
Thanks dear,

I have tried to creating user credential in windows EC2 instance using boto3 script. But i get error like below

"AttributeError: 'ec2.ServiceResource' object has no attribute 'send_command"

Its my Boto3 code

#!/usr/bin/env python3

import boto3
import os
import subprocess

ssm_client = boto3.resource('ec2',aws_access_key_id='xxxxxx',aws_secret_access_key='yyyyy', region_name='us-west-2')

session = boto3.Session (profile_name='exampleaws')

response = ssm_client.send_command(
             InstanceIds=[
                "i-xxxxxx" # use instance id on which you want to execute, even multiple is allowd
                     ],
             DocumentName="AWS-RunPowerShellScript",
             Parameters={
                'commands':[
                     'New-LocalUser "Latchu" -Password "ABCD123" -FullName "LakshmiNarayanan" -Description "User-Details'
                       ]
                   },
             )
command_id = response['Command']['CommandId']
output = ssm_client.get_command_invocation(
      CommandId=command_id,
      InstanceId='i-xxxxxx',
    )
print(output)

--

whats the issue?
Hi,

The send_command attribute is not available in boto3. That's why you got the error.
So , what should i do use instead of response = ssm_client.send_command()

Hi,

I think you just need to add the below line in your code.

ssm_client = boto3.client('ssm')

This module contains the send_command argument.

New-LocalUser "redhat" -Password $Password -FullName "Redhat User" -Description "User for Redhat system"

I need to give a user name and password in the one-line script.

New-LocalUser "redhat" -Password "ABC@123" -FullName "Redhat User" -Description "User for Redhat system"

Shall I proceed like this?

Yes, use the below syntax.

$ New-LocalUser -Name "user_name" -Description "Description of your new account." -NoPassword

If you want to create a user with a password. Then follow the earlier commands.

Related Questions In AWS

0 votes
1 answer

How to Pass the VPC ID while creating the Ec2 instance in AWS using Python Boto3

import boto3 ec2 = boto3.resource('ec2') instance = ec2.create_instances( ...READ MORE

answered Jan 29, 2019 in AWS by Priyaj
• 58,020 points
3,554 views
0 votes
1 answer

How to create an EC2 instance using Python?

Hey JunDevOps, Have a look these scripts: 1. ...READ MORE

answered Jun 24, 2019 in AWS by Aysha

edited Jun 25, 2019 9,295 views
0 votes
1 answer
0 votes
1 answer

How to create an AWS EC2 Instance in the VPC with Boto3 module?

Hi@akhtar, You can use a network interface in ...READ MORE

answered Oct 11, 2020 in AWS by MD
• 95,460 points
2,507 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP