Skip to content

Instantly share code, notes, and snippets.

@adamfortuno
Created July 6, 2021 02:26
Show Gist options
  • Save adamfortuno/54a1ae286f2674bb0072bdc98a65baf2 to your computer and use it in GitHub Desktop.
Save adamfortuno/54a1ae286f2674bb0072bdc98a65baf2 to your computer and use it in GitHub Desktop.
CloudFormation Template to Create a 3 Node Cassandra Cluster
AWSTemplateFormatVersion: '2010-09-09'
Metadata:
License: Apache-2.0
Parameters:
ClusterTargetVPC:
Description: "The name of the VPC to host the cluster."
Type: "String"
ClusterSubnetCIDRRange:
Description: "The address range in the VPC for the cluster"s subnet."
Type: "String"
MinLength: "9"
MaxLength: "18"
AllowedPattern: (\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/(\d{1,2})
ConstraintDescription: Must be a valid CIDR range of the form x.x.x.x/x.
ClusterNodeType:
Description: "The EC2 instance type of the cluster"s nodes. The instance type dictates the hard provisioned for the nodes in the cluster."
Type: "String"
Default: "t2.micro"
AllowedValues:
- "t2.nano"
- "t2.micro"
ConstraintDescription: "This must be a valid AWS EC2 instance type."
ClusterNodeAMI:
Description: "The AMI identifier of the cluster nodes". The AMI dictates the OS deployed to the node."
Type: "AWS::EC2::Image::Id"
ClusterSecurityGroupDescription:
Description: "A description associated with the cluster"s security group."
Type: "String"
Default: "Security group granting clients access to the cluster."
ClusterSecurityGroupPort:
Description: "The port number Cassandra is configured to accept incoming connections on."
Type: "Number"
Default: 9042
MinValue: 1150
MaxValue: 65535
CloseNodeAccessKeyName:
Description: "The name of an existing EC2 key pair for use accessing the cluster nodes."
Type: "String"
ClusterIngressCIDRRange:
Description: "The address range that can be used to communicate with the cluster."
Type: "String"
MinLength: "9"
MaxLength: "18"
Default: "0.0.0.0/0"
AllowedPattern: (\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/(\d{1,2})
ConstraintDescription: Must be a valid CIDR range of the form x.x.x.x/x.
Resources:
ClusterSubnet:
Type: "AWS::EC2::Subnet"
Properties:
VpcId: !Ref "ClusterTargetVPC"
CidrBlock: !Select [0, !Ref ClusterSubnetCIDRRange]
ClusterNode1:
Type: "AWS::EC2::Instance"
Properties:
InstanceType: !Ref "ClusterNodeType"
ImageId: !Ref "ClusterNodeAMI"
KeyName: !Ref "CloseNodeAccessKeyName"
SubnetId: !Ref "ClusterSubnet"
InstanceInitiatedShutdownBehavior: "stop"
SecurityGroups:
- !Ref "ClusterSecurityGroup"
NetworkInterfaces:
- AssociatePublicIpAddress: "true"
DeviceIndex: "0"
GroupSet:
- Ref: "myVPCEC2SecurityGroup"
SubnetId:
Ref: "PublicSubnet"
UserData:
Fn::Base64: |
#!/bin/bash
yum update -y
# Install Java
yum -y install java
echo "export JAVA_HOME=$(whereis java | awk '{print $2}')" >> .bash_profile
# Install Cassandra
echo "[cassandra]" | sudo tee -a /etc/yum.repos.d/cassandra.repo >> /dev/null
echo "name=Apache Cassandra" | sudo tee -a /etc/yum.repos.d/cassandra.repo >> /dev/null
echo "baseurl=https://downloads.apache.org/cassandra/redhat/40x/" | sudo tee -a /etc/yum.repos.d/cassandra.repo >> /dev/null
echo "gpgcheck=1" | sudo tee -a /etc/yum.repos.d/cassandra.repo >> /dev/null
echo "repo_gpgcheck=1" | sudo tee -a /etc/yum.repos.d/cassandra.repo >> /dev/null
echo "gpgkey=https://downloads.apache.org/cassandra/KEYS" | sudo tee -a /etc/yum.repos.d/cassandra.repo >> /dev/null
yum update
yum install cassandra
# Start Cassandra
service cassandra start
ClusterNode2:
Type: "AWS::EC2::Instance"
Properties:
InstanceType: !Ref "ClusterNodeType"
ImageId: !Ref "ClusterNodeAMI"
KeyName: !Ref "CloseNodeAccessKeyName"
SubnetId: !Ref "ClusterSubnet"
InstanceInitiatedShutdownBehavior: "stop"
SecurityGroups:
- !Ref "ClusterSecurityGroup"
UserData:
Fn::Base64: |
#!/bin/bash
yum update -y
# Install Java
yum -y install java
echo "export JAVA_HOME=$(whereis java | awk '{print $2}')" >> .bash_profile
# Install Cassandra
echo "[cassandra]" | sudo tee -a /etc/yum.repos.d/cassandra.repo >> /dev/null
echo "name=Apache Cassandra" | sudo tee -a /etc/yum.repos.d/cassandra.repo >> /dev/null
echo "baseurl=https://downloads.apache.org/cassandra/redhat/40x/" | sudo tee -a /etc/yum.repos.d/cassandra.repo >> /dev/null
echo "gpgcheck=1" | sudo tee -a /etc/yum.repos.d/cassandra.repo >> /dev/null
echo "repo_gpgcheck=1" | sudo tee -a /etc/yum.repos.d/cassandra.repo >> /dev/null
echo "gpgkey=https://downloads.apache.org/cassandra/KEYS" | sudo tee -a /etc/yum.repos.d/cassandra.repo >> /dev/null
yum update
yum install cassandra
# Start Cassandra
service cassandra start
ClusterNode3:
Type: "AWS::EC2::Instance"
Properties:
InstanceType: !Ref "ClusterNodeType"
ImageId: !Ref "ClusterNodeAMI"
KeyName: !Ref "CloseNodeAccessKeyName"
SubnetId: !Ref "ClusterSubnet"
InstanceInitiatedShutdownBehavior: "stop"
SecurityGroups:
- !Ref "ClusterSecurityGroup"
UserData:
Fn::Base64: |
#!/bin/bash
yum update -y
# Install Java
yum -y install java
echo "export JAVA_HOME=$(whereis java | awk '{print $2}')" >> .bash_profile
# Install Cassandra
echo "[cassandra]" | sudo tee -a /etc/yum.repos.d/cassandra.repo >> /dev/null
echo "name=Apache Cassandra" | sudo tee -a /etc/yum.repos.d/cassandra.repo >> /dev/null
echo "baseurl=https://downloads.apache.org/cassandra/redhat/40x/" | sudo tee -a /etc/yum.repos.d/cassandra.repo >> /dev/null
echo "gpgcheck=1" | sudo tee -a /etc/yum.repos.d/cassandra.repo >> /dev/null
echo "repo_gpgcheck=1" | sudo tee -a /etc/yum.repos.d/cassandra.repo >> /dev/null
echo "gpgkey=https://downloads.apache.org/cassandra/KEYS" | sudo tee -a /etc/yum.repos.d/cassandra.repo >> /dev/null
yum update
yum install cassandra
# Start Cassandra
service cassandra start
ClusterSecurityGroup:
Type: "AWS::EC2::SecurityGroup"
Properties:
GroupDescription: !Ref "ClusterSecurityGroupDescription"
SecurityGroupIngress:
- CidrIp: !Ref "ClusterIngressCIDRRange"
FromPort: !Ref "ClusterSecurityGroupPort"
ToPort: !Ref "ClusterSecurityGroupPort"
IpProtocol: "tcp"
Outputs:
Node1Identifier:
Description: "The identifier for node 1."
Value: !Ref "ClusterNode1"
Node1DNS:
Description: "The public domain name for node 1."
Value: !GetAtt [ClusterNode1, PublicDnsName]
Node1IP:
Description: "The public IP address for node 1."
Value: !GetAtt [ClusterNode1, PublicIp]
Node2Identifier:
Description: "The identifier for node 2."
Value: !Ref "ClusterNode1"
Node2DNS:
Description: "The public domain name for node 2."
Value: !GetAtt [ClusterNode2, PublicDnsName]
Node2IP:
Description: "The public IP address for node 2."
Value: !GetAtt [ClusterNode2, PublicIp]
Node3Identifier:
Description: "The identifier for node 3."
Value: !Ref "ClusterNode3"
Node3DNS:
Description: "The public domain name for node 3."
Value: !GetAtt [ClusterNode3, PublicDnsName]
Node3IP:
Description: "The public IP address for node 3."
Value: !GetAtt [ClusterNode3, PublicIp]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment