Create and delete AWS Accounts programmatically


In AWS, it’s best apply to create new accounts if, for instance, you need to deploy completely different phases equivalent to dev, qa and prod on your answer. It’s also frequent to create so-called sandbox AWS accounts to offer these new staff for testing AWS companies. There are numerous extra explanation why new AWS accounts must be created.

Now creating such AWS accounts might be fairly time consuming if it needs to be completed manually. With AWS Organizations, the AWS JavaScript SDK and a little bit TypeScript data, the method might be automated for probably the most half. On this submit, I am going to introduce how this works. To do that, I first clarify what AWS Organizations is within the first place after which I introduce the 2 TypeScript scripts that may create and delete the AWS Sandbox accounts utilizing the AWS SDK.

AWS Organizations

AWS Organizations helps to handle the AWS accounts. In so referred to as Organizational Unites (OU) accounts might be grouped collectively. Moreover, safety mechanisms equivalent to insurance policies might be outlined throughout accounts. AWS Organizations provides way more performance and I wish to discuss with the general public documentation. Fascinating for this submit is that there’s additionally a JavaScript SDK wrapper for the AWS Organizations API that we are going to use within the subsequent sections to dynamically create and delete accounts.

createAccount and deleteAccount

I created a small helper library in GitHub to simplify the programmatic creation and deletion of AWS accounts. An AWS SDK script may then be simply constructed equivalent to:

import {
} from "aws-accounts

export async operate primary(): Promise<void> {
 var args = course of.argv.slice(2)

 if (args.size !== 1) {
  throw new Error("Mistaken quantity of arguments")

 const accountName = args[0]

 const response = await createAccount(accountName)
 const accountId = await getAccountIdFromName(accountName)
 if (accountId) {
  await moveAccountToOU(accountId, "ou-zblx-w7yw0qge")
Enter fullscreen mode

Exit fullscreen mode

The code can be seen within the repo here.

The library is a light-weight wrapper round AWS SDK TypeScript v.2 . With await createAccount(accountName) the account is created first. Then with await getAccountIdFromName(accountName) and await moveAccountToOU(accountId) the account is moved to the outlined OU (Organizational Unit).


On this weblog submit I defined why it’s generally essential to create new AWS accounts and the way this may be completed utilizing AWS Organizations and the JavaScript SDK. For this I created a createAccount and a deleteAccount script and offered them right here. Did you discover the submit useful or do you’ve got any recommendations for enchancment? Let me know and write me :).

Because of the DeepL translater (free version) for serving to with translating to english and saving me tons of time :).

I like to work on Open Supply initiatives. A number of my stuff you’ll be able to already use on . In the event you like my work there and my weblog posts, please think about supporting me on:


Buy me a Ko-Fi

And do not forget to go to my website

Add a Comment

Your email address will not be published. Required fields are marked *