我不知道如何将脚本从 s3 拉取到我的 aws 工作区

问题描述 投票:0回答:1

我在将脚本从 s3 存储桶提取到我的 aws 工作区时遇到问题。我无法使用访问密钥或秘密密钥,因为它们似乎是临时的,并且该脚本将由我们组织中的支持团队使用,因此出于安全原因,访问密钥不适合。我相信最好的方法是将一个角色附加到我的工作区以连接到 s3,因此我附加了一个带有策略的角色以将所有脚本拉到我的工作区。但由于某种原因我仍然收到此错误。

D:\脚本测试 ewfile3.ps1:无法从 S3 下载文件:您提供的 AWS 访问密钥 ID 不 存在于我们的记录中。 + 类别信息:未指定:(:) [写入错误]、WriteErrorException +FullyQualifiedErrorId:Microsoft.PowerShell.Commands.WriteErrorException,newfile3.ps1

这里是脚本:

# Import the AWS PowerShell module 
Import-Module AWSPowerShell -Force


# Define the S3 bucket and object key 
$bucketName = "bucket-name" 
$objectKey = "new ad account/fd new ad account V4.ps1" 

# Define the local file path where you want to save the downloaded code 
$localFilePath = "D:\Script Test" # Replace with your desired local file path 

# Download the code from S3 
try {
    Read-S3Object -SecretKey $secretKey -AccessKey $accessKey -Region us-west-2 -BucketName $bucketName -Key $objectKey -File $localFilePath -ErrorAction Stop
    Write-Host "File downloaded successfully from S3."
} 
catch {
    Write-Error "Failed to download file from S3: $_"
    exit 1
}

# Execute the downloaded code 
try {
    & $localFilePath
    Write-Host "Script executed successfully."
} 
catch {
    Write-Error "Failed to execute script: $_"
    exit 1
}

exit 0

此处是附加到工作区角色的策略:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::your-bucket-name/*"
        }
    ]
}

这里是存储桶策略:

{
    "Version": "2012-10-17",
    "Id": "Policy1711030019865",
    "Statement": [
        {
            "Sid": "AllowWorkspaceToGetObject",
            "Effect": "Allow",
            "Principal": {
                "Service": "workspaces.amazonaws.com"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::your-bucket-name/*"
        }
    ]
}

amazon-web-services powershell amazon-s3 workspace
1个回答
0
投票

通过 IAM 角色获取凭证使用 Amazon EC2 元数据服务,该服务不适用于 Amazon Workspaces

授予访问权限的正常方法是将 IAM 用户凭证存储在本地凭证文件中。但是,由于这违反了您的公司政策,您需要:

  1. 使用 aws sts assume-roleaws sts get-session-token
     命令
    在工作区之外
    生成临时凭证
  2. 通过运行
    aws configure
    登录到工作区后提供这些凭据 - 这将 将凭据存储在本地配置文件中,并且它们仅在有限的时间内有效

这与在个人计算机上提供凭证没有什么不同,只是您的策略意味着您只想使用 AWS Security Token Service (STS) 生成的临时凭证,而不是 AWS Identity and Access Management (IAM) 生成的永久凭证。

要为这些凭证授予对 S3 的访问权限,您只需向您正在使用的 IAM 角色或 IAM 用户添加权限。 无需通过存储桶策略授予访问权限。通常,存储桶策略仅在授予公共或跨账户权限时使用。

© www.soinside.com 2019 - 2024. All rights reserved.