0

我正在尝试使用 AWS CDK 通过自定义策略创建具有最小权限的用户,但我坚持标记该用户并创建其访问密钥。

下面是我的代码:

public class Sample extends App {
    public static void main(final String[] args) {
            App app = new App();
            new UserStack(app, "user-stack");
            app.run();
    }

    public static class UserStack extends Stack {
        // Not able to add Tag and Create Access Key
        public UserStack(final App parent, final String name) {
            super(parent, name);

            PolicyStatement statement = new PolicyStatement(PolicyStatementEffect.Allow);
            statement.addResource("*");
            statement.addAction("lambda:UpdateFunctionCode");

            User user = new User(this, "LambdaDeployer", UserProps.builder().withUserName("lambda-deployer").withPath("/").build());
            user.addToPolicy(statement);

            Tag tag = new Tag("Project", "devops");
            // how to tag the user ??

            new CfnOutput(this, "LambdaDeployerOutputAccessKey", CfnOutputProps.builder().withValue("AWS::IAM::AccessKey").build());
            new CfnOutput(this, "LambdaDeployerOutputSecretAccessKey", CfnOutputProps.builder().withValue("AWS::IAM::SecretAccessKey").build());
        }
    }
}
4

1 回答 1

1

您可能必须使用自定义资源才能调用TagUserAPI,因为在 CloudFormation 中本机无法向用户添加标签。

您可以使用 CDK 中的新功能来帮助您创作自定义资源:https ://github.com/awslabs/aws-cdk/pull/1850

于 2019-05-31T14:28:44.337 回答