DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Infrastructure as Code: Exploring Terraform's Dominance
  • LocalStack and Terraform: A Clean Local AWS Setup Guide
  • Why AWS and Azure Handle Data Perimeter Differently
  • AWS vs GCP Security: Best Practices for Protecting Infrastructure, Data, and Networks

Trending

  • YAGNI in the Age of AI Coding
  • OpenCode: Setting Up an Open-Source AI Coding Assistant
  • Closing the Drift Loop: An AI Agent That Drafts Terraform Fixes
  • The Permission Was Valid. The Action Was Not Authorized.

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Autoscaling Groups With Terraform on AWS Part 2: Instance Security Group and Boot Script

Autoscaling Groups With Terraform on AWS Part 2: Instance Security Group and Boot Script

In this post, we shall add a security group to the autoscaling group and an http server to serve the requests.

By 
Emmanouil Gkatziouras
DZone Core CORE ·
Nov. 17, 20 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
18.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Previously, we followed the minimum steps required in order to spin up an autoscaling group in Terraform. In this post, we shall add a security group to the autoscaling group and an http server to serve the requests.

Using our base configuration we shall create the security group for the instances.

Go
 




x
15


 
1
resource "aws_security_group" "instance_security_group" {
2
  name = "autoscalling_security_group"
3
  ingress {
4
    from_port = 8080
5
    to_port = 8080
6
    protocol = "tcp"
7
    cidr_blocks = ["0.0.0.0/0"]
8
  }
9
  egress {
10
    from_port = 0
11
    protocol = "-1"
12
    to_port = 0
13
    cidr_blocks = ["0.0.0.0/0"]
14
  }
15
}



Our instances shall spin up a server listening at port 8080 thus the security port shall allow ingress traffic to that port. Pay attention to the egress. We shall access resources from the internet thus we want to be able to download em.

Then we will just set the security group at the launch configuration.

Go
 




xxxxxxxxxx
1


 
1
resource"aws_launch_configuration" "launch-configuration" {
2
  name = var.launch_configuration_name
3
  image_id = var.image_id
4
  instance_type = var.instance_type
5
  security_groups = ["${aws_security_group.instance_security_group.id}"]
6
}



Now it’s time to spin up a server on those instances. The aws_launch_configuration gives us the option to specify the startup script (user data on aws ec2). I shall use the Apache Ignite server and its http interface.

Go
 




xxxxxxxxxx
1
15


 
1
resource"aws_launch_configuration" "launch-configuration" {
2
  name = var.launch_configuration_name
3
  image_id = var.image_id
4
  instance_type = var.instance_type
5
  security_groups = ["${aws_security_group.instance_security_group.id}"]
6
  user_data =  <<-EOF
7
              #!/bin/bash
8
              yum install java unzip -y
9
              curl https://www-eu.apache.org/dist/ignite/2.7.6/apache-ignite-2.7.6-bin.zip -o apache-ignite.zip
10
              unzip apache-ignite.zip -d /opt/apache-ignite
11
              cd /opt/apache-ignite/apache-ignite-2.7.6-bin/
12
              cp -r libs/optional/ignite-rest-http/ libs/ignite-rest-http/
13
              ./bin/ignite.sh ./examples/config/example-cache.xml
14
              EOF
15
 
          



And now we are ready to spin up the autoscaling as shown previously.

Shell
 




xxxxxxxxxx
1


 
1
> terraform init
2
> terraform apply



We successfully added an instance security group and a bootstrap script. The next challenge is to add some load balancing and health checks.

security AWS Autoscaling Terraform (software)

Published at DZone with permission of Emmanouil Gkatziouras. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Infrastructure as Code: Exploring Terraform's Dominance
  • LocalStack and Terraform: A Clean Local AWS Setup Guide
  • Why AWS and Azure Handle Data Perimeter Differently
  • AWS vs GCP Security: Best Practices for Protecting Infrastructure, Data, and Networks

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook