Visual Studio Code Aws



  1. Visual Studio Code Aws Setup
  2. Visual Studio Code Aws Cli

The AWS Toolkit for Visual Studio Code is an open source plug-in for the Visual Studio Code that makes it easier to create, debug, and deploy applications on Amazon Web Services. With the AWS Toolkit for Visual Studio Code, you will be able to get started faster and be more productive when building applications with Visual Studio Code on AWS. To access Amazon Web Services (AWS) with the AWS Toolkit for Visual Studio Code, you must make your AWS account credentials available to the toolkit. To use AWS-supported credentials, continue reading in this topic. To use an external credential process, see Using an external credential process.

AWS CloudFormation is an amazing tool for infrastructure as code.

What used to take weeks to do on-premises, is now possible in a few minutes with some JSON, or better yet, YAML.

This doesn’t mean we can’t do better and improve our efficiency even more.

You won’t notice any problems working with a few CloudFormation stacks. A few dozen stacks later, or complicated stacks with many resources though, and we need to start optimizing.

Let’s take a look at how we can up our CloudFormation game with Microsoft’s Visual Studio Code.

This article assumes you are familiar with Visual Studio Code and are using YAML based CloudFormation.

YAML indentation

One thing that always catches out people with editing YAML, is indentation. This is especially true for large CloudFormation files.

Take a look at this snippet. Can you see the error at a quick glance 👀 ?

What about now?

Thanks to the colorization and highlighting of the indent column, it is much easier to see that the Resource property is incorrectly indented at a quick glance.

Visual

To enable this feature, install the following extensions:

Add the following user setting to enable the extra highlighting of the block located at the current cursor position.

Easy 👍.

Tabs, spaces and line endings

Tabs? Spaces? What style of line endings? Use EditorConfig and end all the discussions.

EditorConfig helps maintain consistency across your CloudFormation files by defining rules which the editor will apply on save.

Install the EditorConfig for VSCode extension. Don’t worry, there are plugins for many text editors if the people you work with don’t use VSCode.

Inside your Git repositories that contain your CloudFormation templates, create a .editorconfig file at the root directory that looks like this:

Studio

Every time someone with EditorConfig saves a file, it will update the YAML file according to your rules 💪 .

Sorting things alphabetically

Do you like to keep things sorted alphabetically? It makes it easier when you are looking over massive files or IAM policy statements.

You shouldn’t have to say “A, B, C, D…” in your head every time we want to sort something, though.

Install Sort lines and enjoy the alphabetical awesomeness.

Highlight a block of the CloudFormation template, open the command palette and type > Sort lines.

Visual studio code aws codecommit

Fast access to CloudFormation documentation

If you are like me, you can’t remember the few hundred CloudFormation resource types and properties.

We can use VSCode Tasks to make our lives easier. A task is a simple block of JSON which can execute commands on our machine.

Inside your CloudFormation repositories, create a .vscode folder in the root. Add a tasks.json file with the following content:

⚠️ The above will only work on Mac, you will probably need to call Start-Process as a PowerShell Task on Windows.

From the Command Palette, choose > Tasks: Run Task, and select CF Resource List. This quick launches the AWS Resource Types Reference page.

For the CF Type Search command to work, first highlight a CloudFormation resource type and then from the Command Palette, choose > Tasks: Run Task, and select CF Type Search.

This will take you to the AWS Documentation search page for the resource:

CloudFormation linting

Recently, AWS created a tool called cfn-python-lint, which checks your CloudFormation templates for errors. This gives early feedback and reduces cycle time when creating or updating CloudFormation templates.

Instead of submitting a bad template and waiting for the CloudFormation service tell you its bad, let VS Code tell you as you type!

To install the linter, you will need Python installed. If you are on Windows, I would recommend installing Python from Chocolatey.

Verify the install worked by running cfn-lint --version.

Next, install the vscode-cfn-lint plugin.

Now when you are editing a CloudFormation template, you will get issues underlined and Problems listed when you make a mistake.

✅ You should also use cfn-lint as part of your validation of pull requests on your CloudFormation repositories.

Amplify

Jumping around CloudFormation templates quickly

Got a template with a few thousand lines? Navigating it, and finding the block you are looking for, soon becomes a scroll or search-fest.

First, install the YAML Support by Red Hat extension.

Unfortunately, due to a bug 🐛 we are going to need to disable the YAML validation this extension provides. It currently has issues supporting CloudFormation intrinsic functions. I will update this post when this issue is fixed.

Add the following user setting to disable validation.

Visual Studio Code Aws Setup

Visual studio code web api

Inside your YAML file you will be able to see the logical names of all your resources, search through them and quickly jump to the right section of your CloudFormation template.

Conclusion

Even though CloudFormation frees up a HUGE amount of time for you as an Operations engineer, it doesn’t mean you shouldn’t keep optimizing your processes to be as efficient as possible.

Hopefully with these tips, you can go from a CloudFormation user to a CloudFormation rock star ⭐!

A quick post for working on AWS using Visual Studio Code and terraform.

Install Terraform

Download the latest terraform software and install it on your machine. For windows, keep terraform.exe downloaded from this page in C:WindowsSystem32 and that should be enough.

Install Visual Studio Code

Download Microsoft Visual Studio Code software from their website. Install and launch Visual Studio Code. It supports many code languages. But many of them are not supported on install. One needs to add extensions from the extension marketplace to make it available in VSC.

Install Terraform Visual Studio Code extension

The official Visual Studio Code extension for Terraform is HashiCorp Terraform. You can visit this link and click on the Install button on the webpage, which in turn opens up VSC and install the extension or follow the below procedure.

  • Launch Visual Studio Code
  • Click on the Extensions shortcut on the left navigation panel.
  • Search terraform in extensions marketplace
  • Select HashiCorp Terraform from search results
  • Click the Install button.

Once installed it will be available to use.

Login to AWS account for terraform

Visual Studio Code Aws Cli

It’s programmatic access you will be configuring to AWS. So for that, you should create Access keys through the IAM AWS console. Once you are ready with a valid AWS account with programmatic access enabled and downloaded its access keys, head back to VSC.

Create a new file main.tf with the below code for login. Make sure you use your own key values in there :

and in terminal below, run terraform init command.

Although hard coding credentials like this is not recommended, you can define them as environment variables or other methods supported by terraform.

This should initialize Terraform to use with AWS. It will download the AWS plugin for Terraform. Sample output –

The next step is scanning existing AWS infra to give an account to make the terraform aware of it. Execute the terraform plan command. This command now will use the access keys defined in the file, login to AWS, and check the existing infra.

Creating sample user in AWS using terraform from Visual Studio Code

Now to test a small command, let’s define a user in the file. If that user existing in your AWS account, you should see the same output as above. If not, terraform should show that a new user needs to be created to match the file’s requirements.

Ensure the user you are using in terraform has permissions to create resources in AWS you are planning to.

Add below code in file, save and run terrafrom plan again.

As you can see, since testuser is not existing in AWS infra, it’s prompting to create one! To apply the changes, run the terraform apply command, and it will execute the operation.

Finally, to verify the user created run terrafrom show command.

Finally, to delete all resources being created by terraform, you can run terrafrom destroy command.

It should ask confirmation and then proceed with deleting resources.





Comments are closed.