CreatePolicy 사용하기
CreatePolicy 사용하기 (SDK/CLI)
CreatePolicy(정책 생성) API를 활용하는 방법을 코드 예시로 정리했어요. 각 예시는 어떤 동작을 하는지, 어떤 파라미터를 쓰는지 순서대로 살펴볼 수 있어요.
출처: 문서
본문
다음 코드 예시들은 CreatePolicy 를 어떻게 사용하는지 보여줘요.
.NET
/// <summary>
/// Create an IAM policy.
/// </summary>
/// <param name="policyName">The name to give the new IAM policy.</param>
/// <param name="policyDocument">The policy document for the new policy.</param>
/// <returns>The new IAM policy object.</returns>
public async Task<ManagedPolicy> CreatePolicyAsync(string policyName, string policyDocument)
{
var response = await _IAMService.CreatePolicyAsync(new CreatePolicyRequest
{
PolicyDocument = policyDocument,
PolicyName = policyName,
});
return response.Policy;
}
GitHub에 더 많은 내용이 있어요. AWS Code Examples Repository에서 전체 예시를 찾아 실행 방법을 배울 수 있어요.
AWS CLI(Bash 스크립트)
###############################################################################
# function errecho
#
# This function outputs everything sent to it to STDERR (standard error output).
###############################################################################
function errecho() {
printf "%s\n" "$*" 1>&2
}
###############################################################################
# function iam_create_policy
#
# This function creates an IAM policy.
#
# Parameters:
# -n policy_name -- The name of the IAM policy.
# -p policy_json -- The policy document.
#
# Returns:
# 0 - If successful.
# 1 - If it fails.
###############################################################################
function iam_create_policy() {
local policy_name policy_document response
local option OPTARG # Required to use getopts command in a function.
# bashsupport disable=BP5008
function usage() {
echo "function iam_create_policy"
echo "Creates an AWS Identity and Access Management (IAM) policy."
echo " -n policy_name The name of the IAM policy."
echo " -p policy_json -- The policy document."
echo ""
}
# Retrieve the calling parameters.
while getopts "n:p:h" option; do
case "${option}" in
n) policy_name="${OPTARG}" ;;
p) policy_document="${OPTARG}" ;;
h)
usage
return 0
;;
\?)
echo "Invalid parameter"
usage
return 1
;;
esac
done
export OPTIND=1
if [[ -z "$policy_name" ]]; then
errecho "ERROR: You must provide a policy name with the -n parameter."
usage
return 1
fi
if [[ -z "$policy_document" ]]; then
errecho "ERROR: You must provide a policy document with the -p parameter."
usage
return 1
fi
response=$(aws iam create-policy \
--policy-name "$policy_name" \
--policy-document "$policy_document" \
--output text \
--query Policy.Arn)
local error_code=${?}
if [[ $error_code -ne 0 ]]; then
aws_cli_error_log $error_code
errecho "ERROR: AWS reports create-policy operation failed.\n$response"
return 1
fi
echo "$response"
}
GitHub에 더 많은 내용이 있어요. AWS Code Examples Repository에서 전체 예시를 찾아 실행 방법을 배울 수 있어요.
C++
Aws::String AwsDoc::IAM::createPolicy(const Aws::String &policyName,
const Aws::String &rsrcArn,
const Aws::Client::ClientConfiguration &clientConfig) {
Aws::IAM::IAMClient iam(clientConfig);
Aws::IAM::Model::CreatePolicyRequest request;
request.SetPolicyName(policyName);
request.SetPolicyDocument(BuildSamplePolicyDocument(rsrcArn));
Aws::IAM::Model::CreatePolicyOutcome outcome = iam.CreatePolicy(request);
Aws::String result;
if (!outcome.IsSuccess()) {
std::cerr << "Error creating policy " << policyName << ": " <<
outcome.GetError().GetMessage() << std::endl;
}
else {
result = outcome.GetResult().GetPolicy().GetArn();
std::cout << "Successfully created policy " << policyName <<
std::endl;
}
return result;
}
Aws::String AwsDoc::IAM::BuildSamplePolicyDocument(const Aws::String &rsrc_arn) {
std::stringstream stringStream;
stringStream << "{"
<< " \"Version\": \"2012-10-17\","
<< " \"Statement\": ["
<< " {"
<< " \"Effect\": \"Allow\","
<< " \"Action\": \"logs:CreateLogGroup\","
<< " \"Resource\": \""
<< rsrc_arn
<< "\""
<< " },"
<< " {"
<< " \"Effect\": \"Allow\","
<< " \"Action\": ["
<< " \"dynamodb:DeleteItem\","
<< " \"dynamodb:GetItem\","
<< " \"dynamodb:PutItem\","
<< " \"dynamodb:Scan\","
<< " \"dynamodb:UpdateItem\""
<< " ],"
<< " \"Resource\": \""
<< rsrc_arn
<< "\""
<< " }"
<< " ]"
<< "}";
return stringStream.str();
}
GitHub에 더 많은 내용이 있어요. AWS Code Examples Repository에서 전체 예시를 찾아 실행 방법을 배울 수 있어요.
AWS CLI
예시 1: 고객 관리형 정책 생성하기
aws iam create-policy \
--policy-name my-policy \
--policy-document file://policy.json
다음 명령은 my-policy라는 고객 관리형 정책을 생성해요. policy.json 파일은 현재 폴더에 있는 JSON 문서로, amzn-s3-demo-bucket이라는 Amazon S3 버킷의 공유 폴더에 대한 읽기 전용 액세스를 허용해요.
{
"Version":"2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:Get*",
"s3:List*"
],
"Resource": [
"arn:aws:s3:::amzn-s3-demo-bucket/shared/*"
]
}
]
}
policy.json 파일의 내용:
{
"Policy": {
"PolicyName": "my-policy",
"CreateDate": "2015-06-01T19:31:18.620Z",
"AttachmentCount": 0,
"IsAttachable": true,
"PolicyId": "ZXR6A36LTYANPAI7NJ5UV",
"DefaultVersionId": "v1",
"Path": "/",
"Arn": "arn:aws:iam::0123456789012:policy/my-policy",
"UpdateDate": "2015-06-01T19:31:18.620Z"
}
}
출력:
예시 2: 설명을 포함한 고객 관리형 정책 생성하기
aws iam create-policy \
--policy-name my-policy \
--policy-document file://policy.json \
--description "This policy grants access to all Put, Get, and List actions for amzn-s3-demo-bucket"
using files as input for string parameters에 대한 더 자세한 내용은 AWS CLI User Guide의 "Specify parameter values for the AWS CLI"를 참고하세요.
다음 명령은 변경할 수 없는 설명(immutable description)을 가진 my-policy라는 고객 관리형 정책을 생성해요.
policy.json 파일은 현재 폴더에 있는 JSON 문서로, amzn-s3-demo-bucket이라는 Amazon S3 버킷의 모든 Put, List, Get 작업에 대한 액세스를 허용해요.
{
"Version":"2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:ListBucket*",
"s3:PutBucket*",
"s3:GetBucket*"
],
"Resource": [
"arn:aws:s3:::amzn-s3-demo-bucket"
]
}
]
}
policy.json 파일의 내용:
{
"Policy": {
"PolicyName": "my-policy",
"PolicyId": "ANPAWGSUGIDPEXAMPLE",
"Arn": "arn:aws:iam::123456789012:policy/my-policy",
"Path": "/",
"DefaultVersionId": "v1",
"AttachmentCount": 0,
"PermissionsBoundaryUsageCount": 0,
"IsAttachable": true,
"CreateDate": "2023-05-24T22:38:47+00:00",
"UpdateDate": "2023-05-24T22:38:47+00:00"
}
}
출력:
예시 3: 태그가 있는 고객 관리형 정책 생성하기
aws iam create-policy \
--policy-name my-policy \
--policy-document file://policy.json \
--tags '{"Key": "Department", "Value": "Accounting"}' '{"Key": "Location", "Value": "Seattle"}'
Idenity-based Policies에 대한 더 자세한 내용은 AWS IAM User Guide의 "Identity-based policies and resource-based policies"를 참고하세요.
다음 명령은 태그가 있는 my-policy라는 고객 관리형 정책을 생성해요. 이 예시는 --tags 파라미터에 다음 JSON 형식 태그를 사용해요: ' { "Key": "Department", "Value": "Accounting"}' ' { "Key": "Location", "Value": "Seattle"}' . 또는 --tags 파라미터를 축약 형식의 태그와 함께 쓸 수도 있어요: 'Key=Department,Value=Accounting Key=Location,Value=Seattle' .
policy.json 파일은 현재 폴더에 있는 JSON 문서로, amzn-s3-demo-bucket이라는 Amazon S3 버킷의 모든 Put, List, Get 작업에 대한 액세스를 허용해요.
{
"Version":"2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:ListBucket*",
"s3:PutBucket*",
"s3:GetBucket*"
],
"Resource": [
"arn:aws:s3:::amzn-s3-demo-bucket"
]
}
]
}
policy.json 파일의 내용:
{
"Policy": {
"PolicyName": "my-policy",
"PolicyId": "ANPAWGSUGIDPEXAMPLE",
"Arn": "arn:aws:iam::12345678012:policy/my-policy",
"Path": "/",
"DefaultVersionId": "v1",
"AttachmentCount": 0,
"PermissionsBoundaryUsageCount": 0,
"IsAttachable": true,
"CreateDate": "2023-05-24T23:16:39+00:00",
"UpdateDate": "2023-05-24T23:16:39+00:00",
"Tags": [
{
"Key": "Department",
"Value": "Accounting"
},
"Key": "Location",
"Value": "Seattle"
{
]
}
}
출력:
Go(SDK for Go V2)
import (
"context"
"encoding/json"
"log"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/iam"
"github.com/aws/aws-sdk-go-v2/service/iam/types"
)
// PolicyWrapper encapsulates AWS Identity and Access Management (IAM) policy actions
// used in the examples.
// It contains an IAM service client that is used to perform policy actions.
type PolicyWrapper struct {
IamClient *iam.Client
}
// PolicyDocument defines a policy document as a Go struct that can be serialized
// to JSON.
type PolicyDocument struct {
Version string
Statement []PolicyStatement
}
// PolicyStatement defines a statement in a policy document.
type PolicyStatement struct {
Effect string
Action []string
Principal map[string]string `json:",omitempty"`
Resource *string `json:",omitempty"`
}
// CreatePolicy creates a policy that grants a list of actions to the specified resource.
// PolicyDocument shows how to work with a policy document as a data structure and
// serialize it to JSON by using Go's JSON marshaler.
func (wrapper PolicyWrapper) CreatePolicy(ctx context.Context, policyName string, actions []string,
resourceArn string) (*types.Policy, error) {
var policy *types.Policy
policyDoc := PolicyDocument{
Version: "2012-10-17",
Statement: []PolicyStatement{{
Effect: "Allow",
Action: actions,
Resource: aws.String(resourceArn),
}},
}
policyBytes, err := json.Marshal(policyDoc)
if err != nil {
log.Printf("Couldn't create policy document for %v. Here's why: %v\n", resourceArn, err)
return nil, err
}
result, err := wrapper.IamClient.CreatePolicy(ctx, &iam.CreatePolicyInput{
PolicyDocument: aws.String(string(policyBytes)),
PolicyName: aws.String(policyName),
})
if err != nil {
log.Printf("Couldn't create policy %v. Here's why: %v\n", policyName, err)
} else {
policy = result.Policy
}
return policy, err
}
GitHub에 더 많은 내용이 있어요. AWS Code Examples Repository에서 전체 예시를 찾아 실행 방법을 배울 수 있어요.
Java(SDK for Java 2.x)
import software.amazon.awssdk.core.waiters.WaiterResponse;
import software.amazon.awssdk.services.iam.model.CreatePolicyRequest;
import software.amazon.awssdk.services.iam.model.CreatePolicyResponse;
import software.amazon.awssdk.services.iam.model.GetPolicyRequest;
import software.amazon.awssdk.services.iam.model.GetPolicyResponse;
import software.amazon.awssdk.services.iam.model.IamException;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.iam.IamClient;
import software.amazon.awssdk.services.iam.waiters.IamWaiter;
/**
* Before running this Java V2 code example, set up your development
* environment, including your credentials.
*
* For more information, see the following documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
*/
public class CreatePolicy {
public static final String PolicyDocument = "{" +
" \"Version\": \"2012-10-17\"," +
" \"Statement\": [" +
" {" +
" \"Effect\": \"Allow\"," +
" \"Action\": [" +
" \"dynamodb:DeleteItem\"," +
" \"dynamodb:GetItem\"," +
" \"dynamodb:PutItem\"," +
" \"dynamodb:Scan\"," +
" \"dynamodb:UpdateItem\"" +
" ]," +
" \"Resource\": \"*\"" +
" }" +
" ]" +
"}";
public static void main(String[] args) {
final String usage = """
Usage:
CreatePolicy <policyName>\s
Where:
policyName - A unique policy name.\s
""";
if (args.length != 1) {
System.out.println(usage);
System.exit(1);
}
String policyName = args[0];
Region region = Region.AWS_GLOBAL;
IamClient iam = IamClient.builder()
.region(region)
.build();
String result = createIAMPolicy(iam, policyName);
System.out.println("Successfully created a policy with this ARN value: " + result);
iam.close();
}
public static String createIAMPolicy(IamClient iam, String policyName) {
try {
// Create an IamWaiter object.
IamWaiter iamWaiter = iam.waiter();
CreatePolicyRequest request = CreatePolicyRequest.builder()
.policyName(policyName)
.policyDocument(PolicyDocument)
.build();
CreatePolicyResponse response = iam.createPolicy(request);
// Wait until the policy is created.
GetPolicyRequest polRequest = GetPolicyRequest.builder()
.policyArn(response.policy().arn())
.build();
WaiterResponse<GetPolicyResponse> waitUntilPolicyExists = iamWaiter.waitUntilPolicyExists(polRequest);
waitUntilPolicyExists.matched().response().ifPresent(System.out::println);
return response.policy().arn();
} catch (IamException e) {
System.err.println(e.awsErrorDetails().errorMessage());
System.exit(1);
}
return "";
}
}
GitHub에 더 많은 내용이 있어요. AWS Code Examples Repository에서 전체 예시를 찾아 실행 방법을 배울 수 있어요.
JavaScript(SDK v3)
import { CreatePolicyCommand, IAMClient } from "@aws-sdk/client-iam";
const client = new IAMClient({});
/**
*
* @param {string} policyName
*/
export const createPolicy = (policyName) => {
const command = new CreatePolicyCommand({
PolicyDocument: JSON.stringify({
Version: "2012-10-17",
Statement: [
{
Effect: "Allow",
Action: "*",
Resource: "*",
},
],
}),
PolicyName: policyName,
});
return client.send(command);
};
GitHub에 더 많은 내용이 있어요. AWS Code Examples Repository에서 전체 예시를 찾아 실행 방법을 배울 수 있어요.
정책을 만들어요.
JavaScript(SDK v2)
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set the region
AWS.config.update({ region: "REGION" });
// Create the IAM service object
var iam = new AWS.IAM({ apiVersion: "2010-05-08" });
var myManagedPolicy = {
Version: "2012-10-17",
Statement: [
{
Effect: "Allow",
Action: "logs:CreateLogGroup",
Resource: "RESOURCE_ARN",
},
{
Effect: "Allow",
Action: [
"dynamodb:DeleteItem",
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:Scan",
"dynamodb:UpdateItem",
],
Resource: "RESOURCE_ARN",
},
],
};
var params = {
PolicyDocument: JSON.stringify(myManagedPolicy),
PolicyName: "myDynamoDBPolicy",
};
iam.createPolicy(params, function (err, data) {
if (err) {
console.log("Error", err);
} else {
console.log("Success", data);
}
});
GitHub에 더 많은 내용이 있어요. AWS Code Examples Repository에서 전체 예시를 찾아 실행 방법을 배울 수 있어요.
Kotlin
suspend fun createIAMPolicy(policyNameVal: String?): String {
val policyDocumentVal = """
{
"Version":"2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"dynamodb:DeleteItem",
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:Scan",
"dynamodb:UpdateItem"
],
"Resource": "*"
}
]
}
""".trimIndent()
val request =
CreatePolicyRequest {
policyName = policyNameVal
policyDocument = policyDocumentVal
}
IamClient.fromEnvironment { region = "AWS_GLOBAL" }.use { iamClient ->
val response = iamClient.createPolicy(request)
return response.policy?.arn.toString()
}
}
GitHub에 더 많은 내용이 있어요. AWS Code Examples Repository에서 전체 예시를 찾아 실행 방법을 배울 수 있어요.
PHP
$uuid = uniqid();
$service = new IAMService();
$listAllBucketsPolicyDocument = "{
\"Version\": \"2012-10-17\",
\"Statement\": [{
\"Effect\": \"Allow\",
\"Action\": \"s3:ListAllMyBuckets\",
\"Resource\": \"arn:aws:s3:::*\"}]
}";
$listAllBucketsPolicy = $service->createPolicy("iam_demo_policy_$uuid", $listAllBucketsPolicyDocument);
echo "Created policy: {$listAllBucketsPolicy['PolicyName']}\n";
/**
* @param string $policyName
* @param string $policyDocument
* @return array
*/
public function createPolicy(string $policyName, string $policyDocument)
{
$result = $this->customWaiter(function () use ($policyName, $policyDocument) {
return $this->iamClient->createPolicy([
'PolicyName' => $policyName,
'PolicyDocument' => $policyDocument,
]);
});
return $result['Policy'];
}
GitHub에 더 많은 내용이 있어요. AWS Code Examples Repository에서 전체 예시를 찾아 실행 방법을 배울 수 있어요.
PowerShell Tools (V4)
예시 1: 현재 AWS 계정에 MySamplePolicy라는 새 IAM 정책을 만들어요. MySamplePolicy.json 파일이 정책 내용을 제공해요. JSON 정책 파일을 제대로 처리하려면 -Raw 스위치 파라미터를 반드시 사용해야 해요.
New-IAMPolicy -PolicyName MySamplePolicy -PolicyDocument (Get-Content -Raw MySamplePolicy.json)
출력:
Arn : arn:aws:iam::123456789012:policy/MySamplePolicy
AttachmentCount : 0
CreateDate : 4/14/2015 2:45:59 PM
DefaultVersionId : v1
Description :
IsAttachable : True
Path : /
PolicyId : LD4KP6HVFE7WGEXAMPLE1
PolicyName : MySamplePolicy
UpdateDate : 4/14/2015 2:45:59 PM
PowerShell Tools (V5)
예시 1: 현재 AWS 계정에 MySamplePolicy라는 새 IAM 정책을 만들어요. MySamplePolicy.json 파일이 정책 내용을 제공해요. JSON 정책 파일을 제대로 처리하려면 -Raw 스위치 파라미터를 반드시 사용해야 해요.
New-IAMPolicy -PolicyName MySamplePolicy -PolicyDocument (Get-Content -Raw MySamplePolicy.json)
출력:
Arn : arn:aws:iam::123456789012:policy/MySamplePolicy
AttachmentCount : 0
CreateDate : 4/14/2015 2:45:59 PM
DefaultVersionId : v1
Description :
IsAttachable : True
Path : /
PolicyId : LD4KP6HVFE7WGEXAMPLE1
PolicyName : MySamplePolicy
UpdateDate : 4/14/2015 2:45:59 PM
Python(Boto3)
def create_policy(name, description, actions, resource_arn):
"""
Creates a policy that contains a single statement.
:param name: The name of the policy to create.
:param description: The description of the policy.
:param actions: The actions allowed by the policy. These typically take the
form of service:action, such as s3:PutObject.
:param resource_arn: The Amazon Resource Name (ARN) of the resource this policy
applies to. This ARN can contain wildcards, such as
'arn:aws:s3:::amzn-s3-demo-bucket/*' to allow actions on all objects
in the bucket named 'amzn-s3-demo-bucket'.
:return: The newly created policy.
"""
policy_doc = {
"Version":"2012-10-17",
"Statement": [{"Effect": "Allow", "Action": actions, "Resource": resource_arn}],
}
try:
policy = iam.create_policy(
PolicyName=name,
Description=description,
PolicyDocument=json.dumps(policy_doc),
)
logger.info("Created policy %s.", policy.arn)
except ClientError:
logger.exception("Couldn't create policy %s.", name)
raise
else:
return policy
GitHub에 더 많은 내용이 있어요. AWS Code Examples Repository에서 전체 예시를 찾아 실행 방법을 배울 수 있어요.
Ruby
# Manages policies in AWS Identity and Access Management (IAM)
class RolePolicyManager
# Initialize with an AWS IAM client
#
# @param iam_client [Aws::IAM::Client] An initialized IAM client
def initialize(iam_client, logger: Logger.new($stdout))
@iam_client = iam_client
@logger = logger
@logger.progname = 'PolicyManager'
end
# Creates a policy
#
# @param policy_name [String] The name of the policy
# @param policy_document [Hash] The policy document
# @return [String] The policy ARN if successful, otherwise nil
def create_policy(policy_name, policy_document)
response = @iam_client.create_policy(
policy_name: policy_name,
policy_document: policy_document.to_json
)
response.policy.arn
rescue Aws::IAM::Errors::ServiceError => e
@logger.error("Error creating policy: #{e.message}")
nil
end
# Fetches an IAM policy by its ARN
# @param policy_arn [String] the ARN of the IAM policy to retrieve
# @return [Aws::IAM::Types::GetPolicyResponse] the policy object if found
def get_policy(policy_arn)
response = @iam_client.get_policy(policy_arn: policy_arn)
policy = response.policy
@logger.info("Got policy '#{policy.policy_name}'. Its ID is: #{policy.policy_id}.")
policy
rescue Aws::IAM::Errors::NoSuchEntity
@logger.error("Couldn't get policy '#{policy_arn}'. The policy does not exist.")
raise
rescue Aws::IAM::Errors::ServiceError => e
@logger.error("Couldn't get policy '#{policy_arn}'. Here's why: #{e.code}: #{e.message}")
raise
end
# Attaches a policy to a role
#
# @param role_name [String] The name of the role
# @param policy_arn [String] The policy ARN
# @return [Boolean] true if successful, false otherwise
def attach_policy_to_role(role_name, policy_arn)
@iam_client.attach_role_policy(
role_name: role_name,
policy_arn: policy_arn
)
true
rescue Aws::IAM::Errors::ServiceError => e
@logger.error("Error attaching policy to role: #{e.message}")
false
end
# Lists policy ARNs attached to a role
#
# @param role_name [String] The name of the role
# @return [Array<String>] List of policy ARNs
def list_attached_policy_arns(role_name)
response = @iam_client.list_attached_role_policies(role_name: role_name)
response.attached_policies.map(&:policy_arn)
rescue Aws::IAM::Errors::ServiceError => e
@logger.error("Error listing policies attached to role: #{e.message}")
[]
end
# Detaches a policy from a role
#
# @param role_name [String] The name of the role
# @param policy_arn [String] The policy ARN
# @return [Boolean] true if successful, false otherwise
def detach_policy_from_role(role_name, policy_arn)
@iam_client.detach_role_policy(
role_name: role_name,
policy_arn: policy_arn
)
true
rescue Aws::IAM::Errors::ServiceError => e
@logger.error("Error detaching policy from role: #{e.message}")
false
end
end
GitHub에 더 많은 내용이 있어요. AWS Code Examples Repository에서 전체 예시를 찾아 실행 방법을 배울 수 있어요.
이 예시 모듈은 역할 정책을 나열·생성·연결·분리해요.
Rust
pub async fn create_policy(
client: &iamClient,
policy_name: &str,
policy_document: &str,
) -> Result<Policy, iamError> {
let policy = client
.create_policy()
.policy_name(policy_name)
.policy_document(policy_document)
.send()
.await?;
Ok(policy.policy.unwrap())
}
GitHub에 더 많은 내용이 있어요. AWS Code Examples Repository에서 전체 예시를 찾아 실행 방법을 배울 수 있어요.
SAP ABAP
TRY.
oo_result = lo_iam->createpolicy(
iv_policyname = iv_policy_name
iv_policydocument = iv_policy_document
iv_description = iv_description ).
MESSAGE 'Policy created successfully.' TYPE 'I'.
CATCH /aws1/cx_iamentityalrdyexex.
MESSAGE 'Policy already exists.' TYPE 'E'.
CATCH /aws1/cx_iammalformedplydocex.
MESSAGE 'Policy document is malformed.' TYPE 'E'.
CATCH /aws1/cx_iamlimitexceededex.
MESSAGE 'Policy limit exceeded.' TYPE 'E'.
ENDTRY.
GitHub에 더 많은 내용이 있어요. AWS Code Examples Repository에서 전체 예시를 찾아 실행 방법을 배울 수 있어요.
Swift
import AWSIAM
import AWSS3
public func createPolicy(name: String, policyDocument: String) async throws -> IAMClientTypes.Policy {
let input = CreatePolicyInput(
policyDocument: policyDocument,
policyName: name
)
do {
let output = try await iamClient.createPolicy(input: input)
guard let policy = output.policy else {
throw ServiceHandlerError.noSuchPolicy
}
return policy
} catch {
print("ERROR: createPolicy:", dump(error))
throw error
}
}
GitHub에 더 많은 내용이 있어요. AWS Code Examples Repository에서 전체 예시를 찾아 실행 방법을 배울 수 있어요.