Cloudformation 模板无法执行挂载目标的检索

Cloudformation template is unable to execute retrieval of mount target

我的 cloudformation 模板的用户数据中有以下命令:

MOUNT_TARGET_IP=$(aws efs describe-mount-targets --file-system-id fs-xxxxxxx --query 'MountTargets[*].IpAddress' --output text)

同样在我的模板中,我有以下政策:

MyPolicy:
Type: "AWS::IAM::Policy"
Properties:
  PolicyName: !Sub "${AWS::StackName}_bucket_and_mount_targets_policy"
  PolicyDocument: 
    Version: "2012-10-17"
    Statement: 
      -
        Effect: "Allow"
        Action: "s3:GetObject"
        Resource: !Sub "arn:aws:s3:::${AuthorizedKeyBucketName}/authorized_keys"
      -
        Effect: "Allow"
        Action: "s3:ListBucket"
        Resource: !Sub "arn:aws:s3:::${AuthorizedKeyBucketName}"
      -
        Effect: "Allow"
        Action: "elasticfilesystem:DescribeMountTargets"
        Resource: "arn:aws:elasticfilesystem:us-east-1:xxxxxxxxxx:file-system/fs-xxxxxxx"
  Roles: 
    - 
      !Ref MyRole

我不确定为什么我仍然收到以下错误:

You must specify a region. You can also configure your region by running "aws configure".

当我 ssh 进入实例,然后手动配置区域和访问密钥,然后尝试执行上述语句时,似乎工作正常。

有什么想法吗?

大多数 AWS CLI 命令都需要配置区域,而您尚未在此处配置。

您可以更改 CLI 命令以指定区域:

MOUNT_TARGET_IP=$(aws efs describe-mount-targets \
  --file-system-id fs-xxxxxxx \
  --query 'MountTargets[*].IpAddress' \
  --region ... \  # add this
  --output text)

或者您可以设置 AWS_DEFAULT_REGION 变量:

AWS_DEFAULT_REGION=...
export AWS_DEFAULT_REGION
MOUNT_TARGET_IP=$(aws efs describe-mount-targets --file-system-id fs-xxxxxxx \
  --query 'MountTargets[*].IpAddress' --output text)

或者您可以让脚本 运行 aws 配置或以其他方式提供 ~/.aws/config 中的区域。

请注意,不建议使用访问密钥配置 AWS CLI。您应该改用 IAM 角色。