|
| 1 | +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | + |
| 5 | +package com.example.s3.batch; |
| 6 | + |
| 7 | +import software.amazon.awssdk.regions.Region; |
| 8 | +import software.amazon.awssdk.services.iam.IamClient; |
| 9 | +import software.amazon.awssdk.services.iam.model.CreateRoleRequest; |
| 10 | +import software.amazon.awssdk.services.iam.model.PutRolePolicyRequest; |
| 11 | + |
| 12 | +public class CreateObjectLockRole { |
| 13 | + public static void main(String[] args) { |
| 14 | + final String usage = """ |
| 15 | +
|
| 16 | + Usage: <roleName> |
| 17 | +
|
| 18 | + Where: |
| 19 | + roleName - the IAM role name. |
| 20 | + """; |
| 21 | + |
| 22 | + if (args.length != 1) { |
| 23 | + System.out.println(usage); |
| 24 | + System.exit(1); |
| 25 | + } |
| 26 | + String roleName = args[0]; |
| 27 | + createLockRole(roleName); |
| 28 | + } |
| 29 | + |
| 30 | + // snippet-start:[S3Lock.javav2.lock.role.main] |
| 31 | + /** |
| 32 | + * Creates an IAM role for AWS S3 Batch Operations to manage object locks. |
| 33 | + */ |
| 34 | + public static void createLockRole(String roleName) { |
| 35 | + // Trust policy |
| 36 | + final String trustPolicy = """ |
| 37 | + { |
| 38 | + "Version": "2012-10-17", |
| 39 | + "Statement": [ |
| 40 | + { |
| 41 | + "Effect": "Allow", |
| 42 | + "Principal": { |
| 43 | + "Service": "batchoperations.s3.amazonaws.com" |
| 44 | + }, |
| 45 | + "Action": "sts:AssumeRole" |
| 46 | + } |
| 47 | + ] |
| 48 | + } |
| 49 | + """; |
| 50 | + |
| 51 | + |
| 52 | + // Permissions policy |
| 53 | + final String bopsPermissions = """ |
| 54 | + { |
| 55 | + "Version": "2012-10-17", |
| 56 | + "Statement": [ |
| 57 | + { |
| 58 | + "Effect": "Allow", |
| 59 | + "Action": "s3:GetBucketObjectLockConfiguration", |
| 60 | + "Resource": "arn:aws:s3:::amzn-s3-demo-manifest-bucket" |
| 61 | + }, |
| 62 | + { |
| 63 | + "Effect": "Allow", |
| 64 | + "Action": [ |
| 65 | + "s3:GetObject", |
| 66 | + "s3:GetObjectVersion", |
| 67 | + "s3:GetBucketLocation" |
| 68 | + ], |
| 69 | + "Resource": "arn:aws:s3:::amzn-s3-demo-manifest-bucket/*" |
| 70 | + }, |
| 71 | + { |
| 72 | + "Effect": "Allow", |
| 73 | + "Action": [ |
| 74 | + "s3:PutObject", |
| 75 | + "s3:GetBucketLocation" |
| 76 | + ], |
| 77 | + "Resource": "arn:aws:s3:::amzn-s3-demo-completion-report-bucket/*" |
| 78 | + } |
| 79 | + ] |
| 80 | + } |
| 81 | + """; |
| 82 | + |
| 83 | + // Create IAM client |
| 84 | + final IamClient iam = IamClient.builder() |
| 85 | + .region(Region.US_WEST_2) |
| 86 | + .build(); |
| 87 | + |
| 88 | + // Create the role with the trust policy |
| 89 | + final CreateRoleRequest createRoleRequest = CreateRoleRequest.builder() |
| 90 | + .assumeRolePolicyDocument(trustPolicy) |
| 91 | + .roleName(roleName) |
| 92 | + .build(); |
| 93 | + |
| 94 | + iam.createRole(createRoleRequest); |
| 95 | + |
| 96 | + // Attach the permissions policy to the role |
| 97 | + final PutRolePolicyRequest putRolePolicyRequest = PutRolePolicyRequest.builder() |
| 98 | + .policyDocument(bopsPermissions) |
| 99 | + .policyName("batch_operations-permissions") |
| 100 | + .roleName(roleName) |
| 101 | + .build(); |
| 102 | + |
| 103 | + iam.putRolePolicy(putRolePolicyRequest); |
| 104 | + System.out.println("The object lock role was created."); |
| 105 | + } |
| 106 | + // snippet-end:[S3Lock.javav2.lock.role.main] |
| 107 | +} |
0 commit comments