【问题标题】:How to create cloud formation using IAM roles in AWS Java SDK?如何使用 AWS Java SDK 中的 IAM 角色创建云形成?
【发布时间】:2017-12-02 06:34:59
【问题描述】:

我对@9​​87654321@ 技术非常陌生 我目前的任务是使用具有IAM 角色的Java SDKAmazon Cloud Formation 上创建一个堆栈。在 AWS CLI 上,我可以通过添加额外的参数 --profile 来创建亚马逊云结构。我已经在配置文件中创建了一个带有 role-arn 的配置文件,如下面的link 所述。

现在我想使用来自 AWS 的 Java SDK 来实现相同的功能。我在 Java 中的 Stack request 如下

CreateStackRequest r = new CreateStackRequest();
r.withStackName(getStackName());
r.withParameters(getParameters());
r.withTemplateURL(getTemplate());
r.withCapabilities(getCapabilities());
r.withRoleARN(getArnRole());

我的亚马逊云形成客户端初始化如下

amazonClient=AmazonCloudFormationClientBuilder.standard()
             .withCredentials(new ProfileCredentialsProvider())
             .withRegion(Regions.US_EAST_1)
             .build();

但我无法创建亚马逊云层,因为它给了我以下错误

Exception in thread "main" com.amazonaws.services.cloudformation.model.AmazonCloudFormationException:
User: arn:aws:iam::xxxxxxx:user/xxxxxxx is not authorized to perform: iam:PassRole
on resource: arn:aws:iam::xxxxx:role/xxxxxxxx (Service: AmazonCloudFormation;
Status Code: 403; Error Code: AccessDenied; Request ID: xxxxxxxxxx)

谁能告诉我我做错了什么?

编辑:

AWS CLI

我已在本地 Windows 系统上安装 AWS SDK。要在 aws cli 上执行云形成命令,我正在执行以下操作

aws cloudformation create-stack  --stack-name xxxxx
--template-url xxxxxxxx 
--capabilities "CAPABILITY_IAM" --parameters xxxxxx --profile xxxxxxx

template and parameters 以 json 格式存储在 s3 存储桶中。当我运行上面的命令行时,我得到了以下output

{
  "StackId": "xxxxxxx"
}

AWS Java 开发工具包

我创建了一个 Java 代码,它将以下内容作为命令行参数

--stack-name xxxxxx--template-url xxxxx 
--capabilities "CAPABILITY_IAM" --parameters xxxxx 
--profile xxxxxx --access-key xxxxxxx --secret-key xxxxxxxx

我的AWS config file如下

 [default]
 output = json
 region = us-east-1
 [profile xxxxx]
 role_arn = arn:aws:iam::xxxxxxx:role/xxxxxxxx
 source_profile = default
 region = us-east-1

我的AWS credentials file如下

 [default]
 aws_access_key_id = xxxxxx
 aws_secret_access_key = xxxxxx
 [profile xxxxxx]
 aws_access_key_id = xxxxxx
 aws_secret_access_key = xxxxxxx

亚马逊云形成客户端initialisation,我尝试了以下

 1. amazonClient=AmazonCloudFormationClientBuilder.standard()
             .withCredentials(new ProfileCredentialsProvider())
             .withRegion(Regions.US_EAST_1)
             .build();

 2. BasicAWSCredentials  credentials=new BasicAWSCredentials(accessKey,secretKey); 
   AmazonCloudFormationClientBuilder.standard().withCredentials(new 
   AWSStaticCredentialsProvider(credentials)).build();

initialisations 中,我得到了相同的错误

【问题讨论】:

  • 您需要 ~/.aws/credentials 文件夹中的权限才能完成任务。一些参考:docs.aws.amazon.com/cli/latest/userguide/…
  • @chenrui 如何在 Windows 机器中授予该文件的权限?
  • 权限(IAM 角色)与您的 AWS 账户 ID 相关联,
  • 这段代码是从哪里运行的?本地或任何 EC2 实例?
  • @kosa 本地机器

标签: java amazon-web-services aws-sdk aws-cli amazon-cloudformation


【解决方案1】:

您可以使用 AWS CloudFormation Java API V2 创建新的 Cloud Formation 堆栈。要运行此代码,您必须将模板放入 S3 存储桶中。此外,您必须设置具有 CloudFormation、S3 和 EC2 权限的 IAM 角色。

以下代码成功创建了一个 Stack。

// snippet-start:[cf.java2.create_stack.import]
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.cloudformation.CloudFormationClient;
import software.amazon.awssdk.services.cloudformation.model.CloudFormationException;
import software.amazon.awssdk.services.cloudformation.model.CreateStackRequest;
import software.amazon.awssdk.services.cloudformation.model.OnFailure;
import software.amazon.awssdk.services.cloudformation.model.CreateStackResponse;
import software.amazon.awssdk.services.cloudformation.model.Parameter;
// snippet-end:[cf.java2.create_stack.import]

/**
 *  To run this example, you must have a valid template that is located in a S3 bucket.
 *  For example:
 *
 *  https://s3.amazonaws.com/mybucket/CloudFormationTemplate.yml
 *
 *  Also, the role that you use must have CloudFormation permissions as well as S3 and EC2 permissions. For more information,
 *  see "Getting started with AWS CloudFormation" in the AWS CloudFormation User Guide.
 *
 */

public class CreateStack {

   public static void main(String[] args) {


        String stackName = "mystack2";
        String roleARN = "arn:aws:iam::<enter ARN Role>";
        String location = "https://s3.amazonaws.com/<BUCKET NAME>/CloudFormationTemplate.yml";

        Region region = Region.US_EAST_1;
        CloudFormationClient cfClient = CloudFormationClient.builder()
                .region(region)
                .build();

        try {
            
            // Ensure you set the correct key name and value
            Parameter myParameter = Parameter.builder()
                    .parameterKey("KeyName")
                    .parameterValue("keypair1")
                    .build();

            CreateStackRequest stackRequest = CreateStackRequest.builder()
                .stackName(stackName)
                .templateURL(location)
                .roleARN(roleARN)
                .onFailure(OnFailure.ROLLBACK)
                .parameters(myParameter)
                .build();

            CreateStackResponse stackResponse = cfClient.createStack(stackRequest);
            System.out.println("The stack Id value is " +stackResponse.stackId());

        } catch (CloudFormationException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-20
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-25
    • 2018-11-18
    相关资源
    最近更新 更多