Skip to content

Commit 649e09b

Browse files
scmacdonrlhagerm
authored andcommitted
added a locl policy example
1 parent 415da27 commit 649e09b

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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.lockscenario;
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+
createLockRole();
15+
}
16+
17+
// snippet-start:[S3Lock.javav2.lock.role.main]
18+
/**
19+
* Creates an IAM role for AWS S3 Batch Operations to manage object locks.
20+
*/
21+
public static void createLockRole() {
22+
final String roleName = "batch_operations-object-lock1";
23+
24+
// Trust policy
25+
final String trustPolicy = "{"
26+
+ "\"Version\":\"2012-10-17\","
27+
+ "\"Statement\":[{"
28+
+ "\"Effect\":\"Allow\","
29+
+ "\"Principal\":{"
30+
+ "\"Service\":\"batchoperations.s3.amazonaws.com\""
31+
+ "},"
32+
+ "\"Action\":\"sts:AssumeRole\""
33+
+ "}]"
34+
+ "}";
35+
36+
// Permissions policy
37+
final String bopsPermissions = "{"
38+
+ "\"Version\":\"2012-10-17\","
39+
+ "\"Statement\":["
40+
+ "{"
41+
+ "\"Effect\":\"Allow\","
42+
+ "\"Action\":\"s3:GetBucketObjectLockConfiguration\","
43+
+ "\"Resource\":\"arn:aws:s3:::amzn-s3-demo-manifest-bucket\""
44+
+ "},"
45+
+ "{"
46+
+ "\"Effect\":\"Allow\","
47+
+ "\"Action\":[\"s3:GetObject\",\"s3:GetObjectVersion\",\"s3:GetBucketLocation\"],"
48+
+ "\"Resource\":\"arn:aws:s3:::amzn-s3-demo-manifest-bucket/*\""
49+
+ "},"
50+
+ "{"
51+
+ "\"Effect\":\"Allow\","
52+
+ "\"Action\":[\"s3:PutObject\",\"s3:GetBucketLocation\"],"
53+
+ "\"Resource\":\"arn:aws:s3:::amzn-s3-demo-completion-report-bucket/*\""
54+
+ "}"
55+
+ "]"
56+
+ "}";
57+
58+
// Create IAM client
59+
final IamClient iam = IamClient.builder()
60+
.region(Region.US_WEST_2)
61+
.build();
62+
63+
// Create the role with the trust policy
64+
final CreateRoleRequest createRoleRequest = CreateRoleRequest.builder()
65+
.assumeRolePolicyDocument(trustPolicy)
66+
.roleName(roleName)
67+
.build();
68+
69+
iam.createRole(createRoleRequest);
70+
71+
// Attach the permissions policy to the role
72+
final PutRolePolicyRequest putRolePolicyRequest = PutRolePolicyRequest.builder()
73+
.policyDocument(bopsPermissions)
74+
.policyName("batch_operations-permissions")
75+
.roleName(roleName)
76+
.build();
77+
78+
iam.putRolePolicy(putRolePolicyRequest);
79+
System.out.println("The object lock role was created.");
80+
}
81+
// snippet-end:[S3Lock.javav2.lock.role.main]
82+
}

0 commit comments

Comments
 (0)