Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

AWS System Manger batch operation of EC2 instances

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report