Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

conditionally create instance profile and attach role to it #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 52 additions & 1 deletion copy-role.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ let iam
const sourceRole = await fetchRole(sourceRoleName)
const inlinePolicies = await fetchInlinePolicies(sourceRoleName)
const managedPolicies = await fetchManagedPolicies(sourceRoleName)

const instanceProfiles = await fetchInstanceProfiles(sourceRoleName);

await createRoleFromExisting(sourceRole, targetRoleName)

if (inlinePolicies.length > 0) {
Expand All @@ -21,6 +22,12 @@ let iam
await addManagedPolicies(targetRoleName, managedPolicies)
}

if (instanceProfiles.length === 1 && instanceProfiles[0].InstanceProfileName === sourceRoleName) {
// The source role had a default instance profile with the same name as the role,
// so let's also create the equivalent for the new role.
await createMatchingInstanceProfile(targetRoleName);
}

log('\nDone!')
} catch (e) {
error(e.message)
Expand Down Expand Up @@ -139,6 +146,21 @@ async function fetchManagedPolicies(roleName) {
}
}

async function fetchInstanceProfiles(roleName) {
log('\n--> Fetching source instance profiles...')

let role
try {
role = (await getIam().listInstanceProfilesForRole({RoleName: roleName}).promise()).InstanceProfiles
} catch (e) {
throw new Error(`<-- Failed to fetch source instance profiles: "${e.message}"`)
}

log('<-- Source instance profiles loaded.')

return role
}

async function createRoleFromExisting(sourceRole, targetRoleName) {
log(`\n--> Creating a new role ${targetRoleName}...`)

Expand Down Expand Up @@ -197,6 +219,35 @@ async function addManagedPolicies(targetRoleName, policies) {
log(`<-- Added ${policies.length} managed policies.`)
}

async function createMatchingInstanceProfile(targetRoleName) {
log(`\n--> Creating a new instance profile ${targetRoleName}...`)

let targetInstanceProfile
try {
targetInstanceProfile = (await getIam().createInstanceProfile({
InstanceProfileName: targetRoleName,
}).promise()).InstanceProfile
} catch (e) {
throw new Error(`<-- Failed to create target instance profile: "${e.message}"`)
}

log(`<-- Created instance profile ${targetRoleName}.`)

try {
await getIam().addRoleToInstanceProfile({
InstanceProfileName: targetRoleName,
RoleName: targetRoleName,
}).promise()
} catch (e) {
throw new Error(`<-- Failed to attach target role to instance profile: "${e.message}"`)
}

log(`<-- Attached role ${targetRoleName} to instance profile.`)

return targetInstanceProfile
}


function getIam() {
if (!iam) {
iam = new AWS.IAM()
Expand Down
94 changes: 68 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.