Skip to main content
In this lesson, we’ll demonstrate how to create and manage a custom security group using Pulumi, ensuring that only the required ports are open for your EC2 instances. You’ll configure a security group with two inbound rules (SSH and HTTP) and one outbound rule (allowing all outbound traffic). We’ll attach this security group to an EC2 instance, and later extend the setup to create multiple instances programmatically. Below is a comprehensive example that illustrates how to create a security group, define its rules, and attach it to an EC2 instance.

1. Creating a Security Group and Attaching It to an EC2 Instance

Start by initializing your Pulumi program. In the code snippet below, we create an S3 Bucket (for demonstration), set up a security group for our web server, configure security rules, and launch an EC2 instance with the security group attached.
After deploying this stack with pulumi up, you might encounter a connection error when testing SSH connectivity. For example:
Ensure that your security group allows SSH access. If you experience a timeout, verify that the ingress rule for port 22 is correctly configured.
You can verify the security group and its rules by reviewing the Pulumi preview output in your terminal:
Once the update is complete, connect to your instance using SSH. When connected, update the package manager and install Nginx:
Check that Nginx is running:
When you navigate to the public IP of your instance in a web browser, you should see the default Nginx welcome page confirming that the server is configured correctly.
The image shows a default "Welcome to nginx!" page, indicating successful installation of the nginx web server, with links for documentation and support.

2. Generating a Clickable DNS URL for Your Instance

Instead of manually copying the public IP address, you can output a clickable URL using the instance’s public DNS. Update your outputs as follows:
After deploying with pulumi up, the output will display similar values:
You can also retrieve the stack outputs at any time by running:

3. Creating Multiple EC2 Instances Using a Loop

To efficiently create multiple EC2 instances, define an array of instance names and iterate over it. In the example below, three instances (“web1”, “web2”, and “web3”) are created, and their public IP addresses are collected for output.
During the next pulumi up execution, Pulumi will detect that the original “web-server” instance is no longer needed. It will remove it and create the three new instances. The terminal output will reflect these changes:
Verify the changes by checking the AWS console for the newly created instances (“web1”, “web2”, and “web3”).

4. Monitoring Your Pulumi Deployment

After running an update, click the provided URL in the output to access the Pulumi dashboard. This dashboard offers a detailed view of the recent update, including resource creation, updates, or deletions. It also provides a comprehensive timeline of configuration changes and deployment events.
The image shows a Pulumi dashboard with a successful update status for a stack named "pulumi-demo/dev," displaying configuration details and recent activity.
The Pulumi dashboard is a powerful tool for tracking your deployment progress and understanding resource changes. Make sure to explore it after every update for better insight.

5. Cleaning Up Resources

When you are finished with the demonstration, you can remove all resources from your stack by running:
This command marks all resources for deletion. The output will look similar to this:
Confirm the prompt to allow Pulumi to clean up the resources created during this demo.
This lesson demonstrated how to create and manage security groups and EC2 instances using Pulumi. From outputting useful connection information to scaling your deployment with a loop, you now have a solid foundation for using Pulumi in your infrastructure projects. Happy coding!

Watch Video