-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverless-redis-ec2.yaml
77 lines (68 loc) · 2.74 KB
/
serverless-redis-ec2.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
AWSTemplateFormatVersion: '2010-09-09'
Description: 'Launch an EC2 instance with Amazon Linux 2023 AMI for Redis'
Parameters:
KeyName:
Description: Name of an existing EC2 KeyPair to enable SSH access to the instance
Type: AWS::EC2::KeyPair::KeyName
ConstraintDescription: Please Provide the name of an existing EC2 KeyPair.
InstanceType:
Description: EC2 instance type For Redis
Type: String
Default: t2.micro
AllowedValues: [t2.nano, t2.micro, t2.small, t2.medium, t2.large, t2.xlarge, t2.2xlarge,
t3.nano, t3.micro, t3.small, t3.medium, t3.large, t3.xlarge, t3.2xlarge,
m4.large, m4.xlarge, m4.2xlarge, m4.4xlarge, m4.10xlarge,
m5.large, m5.xlarge, m5.2xlarge, m5.4xlarge,
c5.large, c5.xlarge, c5.2xlarge, c5.4xlarge, c5.9xlarge,
g3.8xlarge,
r5.large, r5.xlarge, r5.2xlarge, r5.4xlarge, r3.12xlarge,
i3.xlarge, i3.2xlarge, i3.4xlarge, i3.8xlarge,
d2.xlarge, d2.2xlarge, d2.4xlarge, d2.8xlarge]
ConstraintDescription: Please choose a valid instance type.
SecurityGroupID:
Type: AWS::EC2::SecurityGroup::Id
LatestAmiId:
Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
Default: '/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-6.1-x86_64'
SubnetId:
Type: List<AWS::EC2::Subnet::Id>
Resources:
Ec2Instance:
Type: AWS::EC2::Instance
Properties:
InstanceType: !Ref 'InstanceType'
SecurityGroupIds: [!Ref 'SecurityGroupID']
KeyName: !Ref 'KeyName'
ImageId: !Ref 'LatestAmiId'
IamInstanceProfile: 'SSMInstanceProfile'
UserData: !Base64
'Fn::Sub': |
#!/bin/bash -xe
sudo dnf install -y redis6.x86_64
sudo systemctl start redis6
cd /home/ec2-user
echo "redis6-cli -h $(aws cloudformation describe-stacks --stack-name $(aws cloudformation describe-stacks --query 'Stacks[0].StackName' --output text) --query 'Stacks[].Outputs[?OutputKey== ClusterEndpoint].OutputValue' --output text) --tls -p 6379" >> redis.sh
chmod u+x redis.sh
Tags:
- Key: 'Name'
Value: 'Ec2InstanceForRedis'
DependsOn:
- 'RedisServerless'
RedisServerless:
Type: AWS::ElastiCache::ServerlessCache
Properties:
Description: 'Creating a serverless redis cluster'
Engine: 'redis'
SecurityGroupIds: [!Ref 'SecurityGroupID']
ServerlessCacheName: 'RedisServerless'
SubnetIds: !Ref 'SubnetId'
Tags:
- Value: 'Name'
Key: 'RedisServerless'
Outputs:
InstanceId:
Description: InstanceId of the newly created EC2 instance
Value: !Ref 'Ec2Instance'
ClusterEndpoint:
Description: Cluster endpoint of the newly created Redis serverless cluster.
Value: !GetAtt RedisServerless.Endpoint.Address