AWS
CloudFormation
EC2
Bash Script
User-Data

AWS Cloudformation How to reuse bash script placed in user-data parameter when creating EC2?

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

AWS CloudFormation is a service that provides a common language for you to describe and provision all the infrastructure resources in your cloud environment. It allows you to utilize a simple text file to model and provision, in an automated and secure manner, all the resources needed for your applications across all regions and accounts.

When launching EC2 instances, you might have initialization scripts like a bash script that you want to execute. AWS CloudFormation facilitates this setup via the `UserData` property. This article will guide you on how to effectively reuse a bash script in the `UserData` parameter when creating EC2 instances using AWS CloudFormation.

Understanding CloudFormation and UserData

What is CloudFormation?

AWS CloudFormation automates and simplifies the management of cloud infrastructure. Using Infrastructure as Code (IaC), CloudFormation uses a JSON or YAML template, enabling you to deploy, manage, and update multiple AWS resources.

What is UserData?

`UserData` is a property associated with EC2 instances that allows you to pass information to customize the instance during its initialization. This data can be used to run scripts, set environment variables, or install packages.

Reusing Bash Scripts with CloudFormation

Defining a Bash Script

Before we reuse a bash script in CloudFormation, let's first define a generic initialization script:

  • Base64 Encoding: The `UserData` needs to be base64 encoded, which the `Fn::Base64` intrinsic function handles.
  • YAML Multiline: The `!Sub` constructor is used for interpolating variables and maintaining multiline strings.
  • Script Reuse: Reusing scripts is feasible by placing the script in a version control system, storing it in S3, or directly referencing shared scripts within multiple EC2 instances.
  • Separate logic: Maintain modular scripts where different functionalities are distinct scripts. This eases reuse and debugging.
  • Version scripts: Control versions of initialization scripts in a repository such as Git to track changes and revert if necessary.
  • Central repository: Store shared scripts in a common S3 bucket or Git repository for centralized access and management.
  • Secure S3 access: Ensure that S3 buckets storing scripts have the correct access permissions and are encrypted.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design