如何使用AWS CDK for Java为帐户创建IAM主体?

huangapple go评论95阅读模式
英文:

How do I create an IAM Principal for an account using AWS CDK for Java?

问题

我想创建一个类似于以下结构的策略(允许跨账户访问 SNS 主题):

{
  "Statement": [{
    "Effect": "Allow",
    "Principal": {
      "AWS": "111122223333"
    },
    "Action": "sns:Subscribe",
    "Resource": "arn:aws:sns:us-east-2:123456789012:MyTopic"
  }]
}

请问我应该如何使用 Java CDK 创建这个策略?我认为我应该使用 AccountPrincipal,但我找不到一个相关的具有公共构造函数的类。

myTopic.addToResourcePolicy(
    PolicyStatement.Builder
        .create()
        .actions(List.of("sns:Subscribe"))
        .effect(Effect.ALLOW)
        .resources(List.of("arn:aws:sns:us-east-2:123456789012:MyTopic"))
        .principals(List.of(  ...在这里如何创建 Principal... ))
        .build()
);

(注意:代码部分未进行翻译)

英文:

I want to create a policy that looks like this (which will permit cross account access to a SNS topic):

{
  "Statement":[{
    "Effect":"Allow",
    "Principal":{
      "AWS":"111122223333"
    },
    "Action":"sns:Subscribe",
    "Resource":"arn:aws:sns:us-east-2:123456789012:MyTopic"
  }]
}

How do I create this using the Java CDK? I think I should be using AccountPrincipal but I can't find a class that seems relevant that has a public constructor.

myTopic.addToResourcePolicy(
    PolicyStatement.Builder
        .create()
        .actions(List.of("sns:Subscribe"))
        .effect(Effect.ALLOW)
        .resources(List.of("arn:aws:sns:us-east-2:123456789012:MyTopic"))
        .principals(List.of(  ...how do I create a principal here?... ))
        .build()
);

答案1

得分: 0

使用 AccountRootPrincipal(实现了 IPrincipal 接口),示例如下:

List.of(new AccountRootPrincipal())
英文:

Use AccountRootPrincipal (which implements IPrincipal) like below:

List.of(new AccountRootPrincipal())

huangapple
  • 本文由 发表于 2020年4月11日 00:16:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/61144285.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定