DEV Community

Nivetha Gopalakrishnan
Nivetha Gopalakrishnan

Posted on

# Day 1 – Session 2: EC2, Nginx, S3 CLI and Lambda Event Trigger

In Day 1 – Session 2, I continued my AWS learning by working with Amazon EC2, Nginx, Amazon S3, AWS CLI, and AWS Lambda.

1. Create an EC2 Instance

The first task was to create an Amazon EC2 instance.

EC2 (Elastic Compute Cloud) provides virtual servers that can be used to run applications and services in AWS.

Steps

  1. Log in to the AWS Management Console.
  2. Search for EC2.
  3. Click Launch instance.
  4. Enter a name for the instance.
  5. Select the required AMI.
  6. Select the required instance type.
  7. Create or select a key pair.
  8. Configure the network and security group settings.
  9. Launch the instance.

After completing these steps, the EC2 instance was successfully created and started.


2. Connect to the EC2 Instance

After launching the instance, I connected to it using SSH.

For an Ubuntu-based EC2 instance, the command is:

ssh -i "your-key.pem" ubuntu@<EC2-PUBLIC-IP>
Enter fullscreen mode Exit fullscreen mode

After successfully connecting, I was able to access the EC2 server through the terminal.


3. Install Nginx

Next, I installed Nginx on the EC2 instance.

Nginx is a web server that can be used to serve websites and web applications.

For an Ubuntu-based instance, I used:

sudo apt update
Enter fullscreen mode Exit fullscreen mode

Then I installed Nginx:

sudo apt install nginx -y
Enter fullscreen mode Exit fullscreen mode

After installation, I started the Nginx service:

sudo systemctl start nginx
Enter fullscreen mode Exit fullscreen mode

I also enabled Nginx to start automatically when the server starts:

sudo systemctl enable nginx
Enter fullscreen mode Exit fullscreen mode

To check the status of Nginx:

sudo systemctl status nginx
Enter fullscreen mode Exit fullscreen mode

Nginx was successfully installed and running on the EC2 instance.


4. Test Nginx

After installing Nginx, I opened the public IP address of the EC2 instance in a browser.

http://<13-233-163-95>
Enter fullscreen mode Exit fullscreen mode

The default Nginx welcome page was displayed successfully.

This confirmed that the EC2 instance was working as a web server.


5. EC2 and S3 Using AWS CLI

Next, I worked on transferring files between EC2 and S3 using the AWS CLI.

I practiced both directions:

EC2 → S3
S3 → EC2
Enter fullscreen mode Exit fullscreen mode

This helped me understand how EC2 can interact with S3 through command-line commands.


6. Transfer a File from EC2 to S3

First, I created a test file on the EC2 instance.

strangerthings.csv
Enter fullscreen mode Exit fullscreen mode

Then I uploaded the file to the S3 bucket using the AWS CLI:

aws s3 cp strangerthings.csv s3://nivi-22034-15/
Enter fullscreen mode Exit fullscreen mode

The file was successfully transferred from EC2 to S3.

To verify the file in the S3 bucket:

aws s3 ls s3://nivi-22034-15/
Enter fullscreen mode Exit fullscreen mode

The file was listed successfully.

The flow was:

EC2
 |
 | AWS CLI
 v
S3 Bucket
 |
 └── strangerthings.csv
Enter fullscreen mode Exit fullscreen mode

7. Transfer a File from S3 to EC2

Next, I tested the reverse process by downloading a file from S3 to EC2.

I used:

aws s3 cp s3://nivi-22034-15/strangerthings.csv .
Enter fullscreen mode Exit fullscreen mode

The file was downloaded to the current directory of the EC2 instance.

To verify the file:

ls
Enter fullscreen mode Exit fullscreen mode

To view the contents:

cat strangerthings.csv
Enter fullscreen mode Exit fullscreen mode

The file was successfully transferred from S3 to EC2.

The flow was:

S3 Bucket
 |
 | AWS CLI
 v
EC2
 |
 └── strangerthings.csv
Enter fullscreen mode Exit fullscreen mode


8. Create an AWS Lambda Function

After working with EC2 and S3, I moved on to AWS Lambda.

AWS Lambda allows us to run code without directly managing servers.

Steps

  1. Open the AWS Management Console.
  2. Search for Lambda.
  3. Click Create function.
  4. Select Author from scratch.
  5. Enter the function name.
  6. Select the required runtime.
  7. Create the Lambda function.

The Lambda function was successfully created.


9. Configure an S3 Event Trigger

Next, I configured an S3 event trigger for the Lambda function.

The objective was to trigger the Lambda function whenever an object was deleted from the S3 bucket.

Steps

  1. Open the Lambda function.
  2. Go to the Function overview section.
  3. Click Add trigger.
  4. Select S3 as the trigger source.
  5. Select the required S3 bucket.
  6. Configure the event type.
  7. Select the Object Deleted event.
  8. Confirm the configuration.
  9. Add the trigger.

The event flow was:

S3 Bucket
    |
    | Object Deleted
    v
AWS Lambda
    |
    v
Lambda Function Executes
Enter fullscreen mode Exit fullscreen mode


10. Test the S3 Delete Trigger

After configuring the trigger, I tested it by deleting an object from the S3 bucket.

For example:

aws s3 rm s3://nivi-22034-15/test.txt
Enter fullscreen mode Exit fullscreen mode

When the object was deleted, the S3 event triggered the Lambda function.

I then checked the Lambda logs to verify that the function was executed successfully.

This confirmed that the S3 Object Deleted event was successfully connected to the Lambda function.


11. Overall Workflow

The main workflow I implemented during this session was:

                    AWS
                     |
          +----------+----------+
          |                     |
         EC2                   S3
          |                     |
          | AWS CLI             |
          +-------------------->|
          |                     |
          |<--------------------+
          |                     |
          |              Object Deleted
          |                     |
          |                     v
          |                  Lambda
          |                     |
          |                     v
          |              Function Executes
Enter fullscreen mode Exit fullscreen mode

12. What I Learned

In Day 1 – Session 2, I learned:

  • How to create an EC2 instance
  • How to connect to an EC2 instance using SSH
  • How to install Nginx
  • How to start and enable the Nginx service
  • How to access the Nginx server using the EC2 public IP
  • How to use AWS CLI from EC2
  • How to transfer files from EC2 to S3
  • How to transfer files from S3 to EC2
  • How to create an AWS Lambda function
  • How to configure an S3 event trigger
  • How to trigger Lambda when an S3 object is deleted
  • How to verify Lambda execution using logs

Conclusion

Day 1 – Session 2 gave me hands-on experience with EC2, Nginx, S3, AWS CLI, and Lambda.

I started by creating an EC2 instance and installing Nginx. I then used AWS CLI to transfer files between EC2 and S3.

Finally, I created a Lambda function and configured an S3 Object Deleted event trigger. After deleting an object from S3, I verified that the Lambda function was triggered successfully.

This session helped me understand how different AWS services can work together to build cloud-based applications.

AWS #EC2 #S3 #Lambda #Nginx #AWSCLI #CloudComputing #DevOps

Top comments (0)