Get the App
SLTechnology News&Howtos  ›  Servers  › 

AWS System Manger batch operation of EC2 instances

Shulou Source: shulou.com Published: 2022-06-03 00:02:02 10月30日 Update

AWS has a service called AWS System Manager that helps administrators automate the management of AWS resources in batches. For example, I can use his Run Command to batch implement certain operations on a specified group of EC2 instances, such as upgrading the system, executing a script, updating agent, and so on.

By default, this System Manager does not have permission to access EC2, so the first step is to create an IAM role that authorizes System Manager to access EC2. It already provides a policy called AmazonEC2RoleforSSM by default. Create a Role. Just choose this Policy.

The next step is to bind this Role to EC2

If only a few EC2 instances can be operated manually, and if there are hundreds of manual operations, it will be too slow, so we can consider using script configuration.

Install AWSPowerShell, then set the account initialization, and then get the instance and register IAMRole

# Install-Module AWSPowerShell-forceSet-AWSCredential-AccessKey XXXXX-SecretKey YYYYYY-StoreAs test#Get-AWSCredential-ListProfileDetailInitialize-AWSDefaults-ProfileName test-Region ap-southeast-2$ instances=Get-EC2Instance | select-ExpandProperty instancesforeach ($item in $instances) {$iamrole=$item | select-ExpandProperty IamInstanceProfile if ($iamrole-eq $null) {Register-EC2IamInstanceProfile-InstanceId $item.InstanceId-IamInstanceProfile_Arn arn:aws:iam::3861158221239:instance-profile/AmazonEC2RoleForSSM}}

We can determine whether the binding is successful or not by the following script

$filter = New-Object Amazon.EC2.Model.Filter-Property @ {Name = "iam-instance-profile.arn"; Value = "arn:aws:iam::386115804199:instance-profile/AmazonEC2RoleForSSM"} $ec2 = @ (Get-EC2Instance-Filter $filter) $ec2instances = $ec2.instances # returns instances with its attributes$ec2instances.privateipaddress #

After success, we return to the interface of System manager, and we can see all the EC2 instances under Managed Instances.

Finally, I did a simple test.

Click run Command and select AWS-RunPowerShellScript

Enter a simple command

Select several instance machines for testing

Click Run

Check the results in a few seconds.

Tags: Instance script selection success can be passed manually test manage only commands situations machines permissions for example interface administrator system results accounts resources Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Linux Shulou Tech Info Shulou Technology Apple