Connecting AWS EC2 (Elastic Compute Cloud) with AWS SNS (Simple Notification Service) enables seamless message communication between your applications. This integration allows EC2 instances to publish notifications or messages to various subscribers, enhancing your system’s efficiency and responsiveness. Here’s a concise guide to set up this connection.
Step 1: Launch an EC2 Instance
1. Access the AWS Management Console:
Log in to your AWS account and navigate to the EC2 dashboard.
2. Create an EC2 Instance:
Click on Launch Instance and select an Amazon Machine Image (AMI).
Choose an instance type (e.g., t2.micro for the free tier) and configure instance details.
Ensure your security group allows outbound traffic to SNS.
Step 2: Create an SNS Topic
1. Navigate to SNS:
From the AWS Management Console, search for and select SNS.
2. Create a New Topic:
Click on Create topic and choose the type (Standard or FIFO).
Name your topic (e.g., MyNotificationTopic) and set any additional configurations.
Click Create topic.
Step 3: Set Up IAM Permissions
1. Create an IAM Role:
Go to the IAM service, click on Roles, and select Create role.
Choose AWS service and select EC2.
Attach the policy AmazonSNSFullAccess to grant your EC2 instance permissions to publish messages to SNS.
2. Attach the Role to Your EC2 Instance:
Navigate back to the EC2 dashboard, select your instance, and go to Actions > Security > Modify IAM Role.
Attach the IAM role you created.
Step 4: Install AWS SDK on EC2
1. Connect to Your EC2 Instance:
Use SSH to access your EC2 instance:
ssh -i “your-key.pem” ec2-user@your-ec2-public-ip
2. Install AWS SDK:
For Python, ensure you have Python and pip installed. Then, install Boto3:
sudo yum install python3 -y # For Amazon Linux
pip3 install boto3
Step 5: Publish Messages to SNS
1. Create a Python Script:
Write a script (publish_sns.py) to publish messages to your SNS topic:
import boto3
# Create SNS client
sns = boto3.client(‘sns’)
# Replace with your topic ARN
topic_arn = ‘arn:aws:sns:region:123456789012:MyNotificationTopic’
# Publish a message
response = sns.publish(
TopicArn=topic_arn,
Message=’Hello from EC2!’,
)
print(‘Message ID:’, response[‘MessageId’])
2. Run the Script:
Execute your script to send a message to SNS:
python3 publish_sns.py
Step 6: Subscribe to SNS Notifications
1. Create Subscribers:
Back in the SNS console, select your topic and click Create subscription.
Choose a protocol (e.g., Email, HTTP, Lambda) and provide the endpoint (e.g., your email address).
Click Create subscription and confirm the subscription as necessary.
Connecting AWS EC2 with AWS SNS is a straightforward process that enables efficient message dissemination across distributed systems. This setup enhances your application’s responsiveness and scalability, ensuring timely notifications and seamless communication. By following these steps, you can leverage the power of AWS services for your projects effectively.
The article above is rendered by integrating outputs of 1 HUMAN AGENT & 3 AI AGENTS, an amalgamation of HGI and AI to serve technology education globally.