EC2 Creating an instance using user data(AWS)

2023.03.15

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Elastic Cloud Compute (EC2)


Introduction

EC2 is one of the most popular AWS service as it has a capability of renting and storing data in virtual machines (EC2&EBS) and also distributing load across machines (ELB),scaling the services (ASG).The above key features of the EC2 plays a big role in running all the sizes of business successfully. EC2 instances are available in a variety of configurations, each designed for different computing needs. Users can choose from a number of instance types, including but not limited to General Purpose, Compute Optimised, Memory Optimised, and Memory Optimised Instances.

Creating EC2 Instances With EC2 user data

First login to the AWS console and search for EC2 and follow the below images for the process.

  • Select the instances option on the EC2 dashboard page

  • Launch an instance by clicking the "Launch instance" button.

  • Give a name to the EC2 instance
  • Add a EC2 base image here we selected the default option.
  • AMI and Architecture are the default options provided by AWS.

  • Under Advanced Details select the key pair option and create a new key pair.
  • In Create key pair add key pair name ,type and file format
  • Here we selected the default type ,file format
  • click create key pair button.

    • It is downloaded and selected Automatically.
    • In network settings
    • Instance is going to get an public id for that we connect our instance.
    • Select Allow http traffic from the internet
    • Other options let it be default

Code for user data

#!/bin/bash
# Use this for your user data (script from top to bottom)
# install httpd (Linux 2 version)
yum update -y
yum install -y httpd
systemctl start httpd
systemctl enable httpd
echo "<h1>Hello World from $(hostname -f)</h1>" > /var/www/html/index.html
  • Here the above code runs "Hello World" in the web server from EC2 instance
  • The number of Instance that will launch is 1.
  • At the end Launch the Instance.
  • The Instance is launched successfully
  • In the details public ip address is given
  • The web server is running our program with a given public ip address

Conclusion

Overall, EC2 is a powerful tool for anyone who needs on-demand computing resources in the cloud. Whether you are a small business owner, a developer, or a large enterprise, EC2 can help you save time and money while providing the computing power you need to run your applications.