|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package com.example.iam; |
| 5 | + |
| 6 | +import software.amazon.awssdk.services.iam.IamClient; |
| 7 | +import software.amazon.awssdk.services.iam.model.CreateRoleRequest; |
| 8 | +import software.amazon.awssdk.services.iam.model.PutRolePolicyRequest; |
| 9 | + |
| 10 | +/** |
| 11 | + * Before running this Java V2 code example, set up your development |
| 12 | + * environment, including your credentials. |
| 13 | + * |
| 14 | + * For more information, see the following documentation topic: |
| 15 | + * |
| 16 | + * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html |
| 17 | + */ |
| 18 | +public class CreateObjectLockRole { |
| 19 | + |
| 20 | + public static void main(String[] args) { |
| 21 | + final String roleName = "<Enter role name>"; |
| 22 | + IamClient iam = IamClient.builder().build(); |
| 23 | + createObjectLockRole(iam, roleName); |
| 24 | + } |
| 25 | + |
| 26 | + // snippet-start:[iam.java2.s3_role.main] |
| 27 | + /** |
| 28 | + * Creates an IAM role with the necessary permissions to perform object lock operations on an S3 bucket. |
| 29 | + * |
| 30 | + * @param iam An instance of the {@link IamClient} class, which is used to interact with the AWS IAM service. |
| 31 | + * @param roleName The name of the IAM role to be created. |
| 32 | + */ |
| 33 | + public static void createObjectLockRole(IamClient iam, String roleName) { |
| 34 | + final String bopsPermissions = """ |
| 35 | + { |
| 36 | + "Version": "2012-10-17", |
| 37 | + "Statement": [ |
| 38 | + { |
| 39 | + "Effect": "Allow", |
| 40 | + "Action": "s3:GetBucketObjectLockConfiguration", |
| 41 | + "Resource": [ |
| 42 | + "arn:aws:s3:::<ENTER Bucket Name>" |
| 43 | + ] |
| 44 | + }, |
| 45 | + { |
| 46 | + "Effect": "Allow", |
| 47 | + "Action": [ |
| 48 | + "s3:GetObject", |
| 49 | + "s3:GetObjectVersion", |
| 50 | + "s3:GetBucketLocation" |
| 51 | + ], |
| 52 | + "Resource": [ |
| 53 | + "arn:aws:s3:::<ENTER Bucket Name>/*" |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "Effect": "Allow", |
| 58 | + "Action": [ |
| 59 | + "s3:PutObject", |
| 60 | + "s3:GetBucketLocation" |
| 61 | + ], |
| 62 | + "Resource": [ |
| 63 | + "arn:aws:s3:::<ENTER Bucket Name>/*" |
| 64 | + ] |
| 65 | + } |
| 66 | + ] |
| 67 | + }"""; |
| 68 | + |
| 69 | + CreateRoleRequest createRoleRequest = CreateRoleRequest.builder() |
| 70 | + .assumeRolePolicyDocument(bopsPermissions) |
| 71 | + .roleName(roleName) |
| 72 | + .build(); |
| 73 | + |
| 74 | + iam.createRole(createRoleRequest); |
| 75 | + PutRolePolicyRequest putRolePolicyRequest = PutRolePolicyRequest.builder() |
| 76 | + .policyDocument(bopsPermissions) |
| 77 | + .policyName("batch_operations-permissions") |
| 78 | + .roleName(roleName) |
| 79 | + .build(); |
| 80 | + |
| 81 | + iam.putRolePolicy(putRolePolicyRequest); |
| 82 | + } |
| 83 | + // snippet-end:[iam.java2.s3_role.main] |
| 84 | +} |
0 commit comments