iam_attachrolepolicy.ts - AWS Code Sample

iam_attachrolepolicy.ts

/* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 ABOUT THIS NODE.JS EXAMPLE: This example works with AWS SDK for JavaScript version 3 (v3), which is pending release. The preview version of the SDK is available at https://github.com/aws/aws-sdk-js-v3. This example is in the 'AWS SDK for JavaScript v3 Developer Guide' at https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/iam-examples-policies.html. Purpose: iam_attachrolepolicy.ts demonstrates how to attach a managed policy to an IAM role. Inputs : - REGION - ROLE_NAME Running the code: ts-node iam_attachrolepolicy.ts */ // Import required AWS SDK clients and commands for Node.js const { IAMClient, ListAttachedRolePoliciesCommand, AttachRolePolicyCommand } = require("@aws-sdk/client-iam"); // Set the AWS Region const REGION = "REGION"; //e.g. "us-east-1" // Set the parameters const ROLENAME = "ROLE_NAME"; const paramsRoleList = { RoleName: ROLENAME }; //ROLE_NAME // Create IAM service object const iam = new IAMClient(REGION); const run = async () => { const iam = new IAMClient(REGION); try { const data = await iam.send( new ListAttachedRolePoliciesCommand(paramsRoleList) ); const myRolePolicies = data.AttachedPolicies; myRolePolicies.forEach(function (val, index, array) { if (myRolePolicies[index].PolicyName === "AmazonDynamoDBFullAccess") { console.log( "AmazonDynamoDBFullAccess is already attached to this role." ); process.exit(); } }); try { const params = { PolicyArn: "arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess", RoleName: ROLENAME, }; const data = await iam.send(new AttachRolePolicyCommand(params)); console.log("Role attached successfully"); } catch (err) { console.log("Error", err); } } catch (err) { console.log("Error", err); } }; run();