我们使用负载均衡的自动扩展Web服务器(IIS)在Amazon EC2上运行我们的Web应用程序.
在自动扩展之前,我们的部署过程是文件复制到几个大型Web服务器.
现在,通过自动缩放,我们可以随意显示和消失5到12个Web服务器,从而使部署过程更加困难.
为了解决这个问题,我编写了一个powershell脚本,用于检索自动扩展组中的服务器IP,并使用MSDeploy将它们与指定的部署服务器同步(在自动扩展组之外的负载均衡器中).然后,它会创建一个新的AMI并更新自动缩放配置.
一切似乎都很好,在重建部署服务器之后,同步脚本不会更新网站的运行状态.所以我可以将网站置于维护模式.
我想知道:
其他人如何解决问题(特别是在autoscaling ec2中同步iis服务器)(在没有WFF的情况下为IIS 8)
为什么启动/停止同步失败
码:
Set-AWSCredentials -AccessKey XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -SecretKey XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Set-DefaultAWSRegion "us-west-2"
$date = get-date
$dateString = $date.ToString("yyyyMMdd-HHmm")
$name = $dateString + "Web"
$imageId = new-ec2image -InstanceId x-xxxxxxxx -Name $name -NoReboot 1
$launchConfiguration = New-ASLaunchConfiguration -LaunchConfigurationName $name -ImageId $imageId -InstanceType "m3.medium" -SecurityGroups @('Web') -InstanceMonitoring_Enabled $false
Update-AsAutoScalingGroup -AutoScalingGroupName "XxxxxxxxxxxxXxxxxxxxxx" -LaunchConfigurationName $name
$a = Get-ASAutoScalingInstance | select -expandproperty InstanceId | Get-EC2Instance | select -expandproperty RunningInstance | select -property PrivateIpAddress
foreach($ip in $a)
{
$command = …Run Code Online (Sandbox Code Playgroud) 这更像是一种健全性检查,因为我已经解决了这个问题,但我不相信我已经以聪明的方式做到了.
问题
我有一些实例已经分配了一个允许他们访问S3存储桶的IAM角色.然后我需要运行一些PowerShell脚本来访问S3存储桶以下载一些对象.
解决方案
要获取/设置要使用的凭据,我已编写此PowerShell函数:
function Set-MyInstanceProfileCredentials {
param(
[parameter()]
[string]
$StoredCredentialsName = "MyInstanceProfileCredentials"
)
$Uri = "http://169.254.169.254/latest/meta-data/iam/security-credentials/"
Write-Verbose "Retrieving instance profile from $($Uri)"
$Uri = "$Uri$(Invoke-RestMethod -Uri $Uri)"
Write-Verbose "Retrieving security credentials from $($Uri)"
$Response = Invoke-RestMethod -Uri $Uri
Set-AWSCredentials -AccessKey $Response.AccessKey -SecretKey $Response.SecretAccessKey -StoreAs $StoredCredentialsName
Get-AWSCredentials -StoredCredentials $StoredCredentialsName
}
Run Code Online (Sandbox Code Playgroud)
然后,当我需要从AWS模块运行PowerShell cmdlet时,我首先调用此函数.
但是,我无法摆脱我已经错过了AWS PowerShell模块的错误,这个模块已经为我解决了这个问题.
我刚刚下载了最新版本的 AWS Powershell 并尝试了这个:之前我不记得有任何问题。现在我收到此错误消息:
PS C:\Program Files (x86)\AWS Tools\PowerShell\AWSPowerShell> Set-AWSCredentials -AccessKey xxxxxxxx -SecretKey xxxxxxx -StoreAs xxxx
Set-AWSCredentials : The term 'Set-AWSCredentials' is not recognized as the name of a cmdlet, function, script file,
or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and
try again.
At line:1 char:1
+ Set-AWSCredentials -AccessKey xxxxxxx -SecretKey xxxxx
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Set-AWSCredentials:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
PS …Run Code Online (Sandbox Code Playgroud) 我正在使用powershell来调用Get-EC2PasswordData,如下所示:
$instances = (Get-EC2Instance -Filter @($envFilter, $stateFilter)).Instances
$instances | Foreach-object {
$instID = $_.InstanceId
Write-Host Getting password to $instID...
$password = Get-EC2PasswordData -InstanceId $instID -PemFile "c:\my.pem" -Decrypt
Write-Host Username/Password for $_.PrivateIpAddress is Administrator/$password
}
Run Code Online (Sandbox Code Playgroud)
我得到以下内容:
Getting password to i-3e961280 ...
Get-EC2PasswordData : Value cannot be null.
Parameter name: s
At C:\temp\CIS-aws-volumes\copyToMachine.ps1:12 char:17
+ $password = Get-EC2PasswordData -InstanceId $instID -PemFile "c:\docs\ssh\ci ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Amazon.PowerShe...swordDataCmdlet:GetEC2PasswordDataCmdlet) [Get-EC2
PasswordData], ArgumentNullException
+ FullyQualifiedErrorId : InvalidOperationException,Amazon.PowerShell.Cmdlets.EC2.GetEC2PasswordDataCmdlet
Username/Password for 10.185.30.124 is Administrator/ …Run Code Online (Sandbox Code Playgroud) 如何使用Amazon Web Services(AWS)PowerShell模块向EC2实例添加标签(键值对)?
在我的 Jenkins 构建作业中,我使用此命令来承担 AWS 角色:
$Creds = (Use-STSRole -Region us-east-1 -RoleArn arn:aws:iam::$IAM_ACCOUNT_ID`:role/$IAM_ROLE -RoleSessionName jenkins).Credentials
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Use-STSRole :请求中包含的安全令牌无效。
将 AWS 角色更改为无效角色不会更改错误消息。
登录服务器并直接在Powershell中使用命令时,它工作正常。
如果我使用 AWS CLI 命令,它也可以工作:
aws sts assume-role --role-arn arn:aws:iam::%IAM_ACCOUNT_ID%:role/%IAM_ROLE% --role-session-name jenkins-deploy
Run Code Online (Sandbox Code Playgroud)
完整的错误信息:
Use-STSRole : The security token included in the request is invalid.
At C:\Users\svc-jenkins.WIN-KLBFC355P8D\AppData\Local\Temp\jenkins4822311255190032778.ps1:5 char:11
+ $Creds = (Use-STSRole -Region us-east-1 -RoleArn arn:aws:iam::$e ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Amazon.PowerShe...seSTSRoleCmdlet:UseSTSRoleCmdlet) [Use-STSRole], InvalidOperationException
+ FullyQualifiedErrorId : Amazon.SecurityToken.AmazonSecurityTokenServiceException,Amazon.PowerShell.Cmdlets.STS.UseSTSRoleCmdlet
Run Code Online (Sandbox Code Playgroud) powershell amazon-web-services jenkins amazon-iam aws-powershell
我正在使用 powershell 使用以下脚本创建新的 EC2 密钥对:
Import-Module AWSPowerShell
$Region = "EU West"
$ProfileName = "AWS Default"
Initialize-AWSDefaults -ProfileName $ProfileName
$KeyName = "SecurityEssentials"
$KeyPair = New-EC2KeyPair -KeyName $KeyName
Run Code Online (Sandbox Code Playgroud)
但是它在最后一行失败并显示以下消息:
New-EC2KeyPair:无效的 URI:无法解析主机名。在 C:\Users\John\Documents\Cloud\AWS Setup.ps1:12 char:12 + $KeyPair = New-EC2KeyPair -KeyName $KeyName + ~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (Amazon.PowerShe...C2KeyPairCmdlet:NewEC2KeyPairCmdlet) [New-EC2KeyPair], InvalidOperationException +fullyQualifiedErrorId : System.UriFormatException,Amazon.PowerShell.Cmdlets .EC2.NewEC2KeyPairCmdlet
使用调试标志不会提供任何新信息。我正在使用带有最新 AWS SDK 和 AWS powershell cmdlet 的 Windows 8.1
编辑:问题似乎与 AWSDefaults 的设置方式有关。事实证明,在此之后发出的任何命令都会出现相同的错误。
是否有任何简单的方法可以使用 PowerShell 仅从 S3 存储桶获取“文件夹”列表,而无需列出每个对象,而只需编写不同路径的编译列表的脚本?我正在处理的存储桶中有数十万个单独的对象,这将需要很长时间。
这可能是一个非常愚蠢的问题,如果是这样的话,我很抱歉,但我在谷歌或SO上找不到任何东西来回答这个问题。我尝试向 Get-S3Object 的 -KeyPrefix 和 -Key 参数添加通配符,但无济于事。这是唯一一个似乎能够完成我所追求的任务的 cmdlet。
无意义的背景故事:我只是想确保将文件传输到正确的现有文件夹。我是签约第三方,因此我没有控制台登录访问权限,也不是维护 AWS 账户的人。
我知道使用 Java 和 C# 以及其他语言可以做到这一点,但我正在 PS 中完成与这个相当简单的项目相关的所有其他工作,并希望能够坚持下去。
提前致谢。
我正在尝试使用 Powershell 中的 AWS 工具,但我不知道如何登录。因此,当我运行命令时,我自然会收到此错误:
没有从持久化/外壳默认值中指定或获取凭据。
如何登录?我在https://docs.aws.amazon.com/powershell/latest/userguide/specifying-your-aws-credentials.html 上阅读了这篇文章,但它谈到了公钥和私钥。我没有这些,我使用我的用户名和密码从 Web 界面登录 AWS(还有一个令牌发送到我的手机,因为我启用了 MFA)。
此外,我的用户不允许创建 IAM 用户(这是公司政策,我无法覆盖它)。
Powershell 中的第一步。我无法AWSPowerShell 从 AWS 安装的模块中删除/卸载模块。\nGet-Module -Name AWSPowerShell,AWSPowerShell.NetCore,AWS.Tools.Common -ListAvailable显示如下:
Directory: C:\\Users\\user\\Documents\\PowerShell\\Modules\n\nModuleType Version PreRelease Name PSEdition ExportedCommands\n---------- ------- ---------- ---- --------- ----------------\nBinary 4.1.14.0 AWS.Tools.Common Core,Desk {Clear-AWSHistory, Set-AWSHistoryConfiguration, Initialize-AWSDefaultConfi\xe2\x80\xa6\n\n Directory: C:\\Program Files (x86)\\AWS Tools\\PowerShell\n\nModuleType Version PreRelease Name PSEdition ExportedCommands\n---------- ------- ---------- ---- --------- ----------------\nBinary 3.3.509.0 AWSPowerShell Desk\nRun Code Online (Sandbox Code Playgroud)\n我试图通过以下方式卸载它,但Uninstall-Module -Name AWSPowerShell出现错误:
Uninstall-Package: C:\\program files\\powershell\\7\\Modules\\PowerShellGet\\PSModule.psm1:12733\n Line |\n12733 | \xe2\x80\xa6 $null = PackageManagement\\Uninstall-Package @PSBoundParameters\n | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n | No match was found for the specified search criteria and module …Run Code Online (Sandbox Code Playgroud) aws-powershell ×10
powershell ×9
amazon-ec2 ×3
amazon-iam ×2
amazon-s3 ×2
iis ×1
jenkins ×1
msdeploy ×1