Skip to content

Commit cdde4c2

Browse files
Pratik DasPratik Das
authored andcommitted
first code samples
1 parent a672db3 commit cdde4c2

File tree

15 files changed

+1086
-0
lines changed

15 files changed

+1086
-0
lines changed
61.1 KB
Binary file not shown.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.6/apache-maven-3.8.6-bin.zip
18+
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar

aws/structured-logging-cw/iac_aws/manage-VM/.terraform.lock.hcl

Lines changed: 62 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
{
2+
"agent": {
3+
"metrics_collection_interval": 10,
4+
"logfile": "/opt/aws/amazon-cloudwatch-agent/logs/amazon-cloudwatch-agent.log"
5+
},
6+
"metrics": {
7+
"metrics_collected": {
8+
"cpu": {
9+
"resources": [
10+
"*"
11+
],
12+
"measurement": [
13+
{"name": "cpu_usage_idle", "rename": "CPU_USAGE_IDLE", "unit": "Percent"},
14+
{"name": "cpu_usage_nice", "unit": "Percent"},
15+
"cpu_usage_guest"
16+
],
17+
"totalcpu": false,
18+
"metrics_collection_interval": 10,
19+
"append_dimensions": {
20+
"customized_dimension_key_1": "customized_dimension_value_1",
21+
"customized_dimension_key_2": "customized_dimension_value_2"
22+
}
23+
},
24+
"disk": {
25+
"resources": [
26+
"/",
27+
"/tmp"
28+
],
29+
"measurement": [
30+
{"name": "free", "rename": "DISK_FREE", "unit": "Gigabytes"},
31+
"total",
32+
"used"
33+
],
34+
"ignore_file_system_types": [
35+
"sysfs", "devtmpfs"
36+
],
37+
"metrics_collection_interval": 60,
38+
"append_dimensions": {
39+
"customized_dimension_key_3": "customized_dimension_value_3",
40+
"customized_dimension_key_4": "customized_dimension_value_4"
41+
}
42+
},
43+
"diskio": {
44+
"resources": [
45+
"*"
46+
],
47+
"measurement": [
48+
"reads",
49+
"writes",
50+
"read_time",
51+
"write_time",
52+
"io_time"
53+
],
54+
"metrics_collection_interval": 60
55+
},
56+
"swap": {
57+
"measurement": [
58+
"swap_used",
59+
"swap_free",
60+
"swap_used_percent"
61+
]
62+
},
63+
"mem": {
64+
"measurement": [
65+
"mem_used",
66+
"mem_cached",
67+
"mem_total"
68+
],
69+
"metrics_collection_interval": 10
70+
},
71+
"net": {
72+
"resources": [
73+
"eth0"
74+
],
75+
"measurement": [
76+
"bytes_sent",
77+
"bytes_recv",
78+
"drop_in",
79+
"drop_out"
80+
]
81+
},
82+
"netstat": {
83+
"measurement": [
84+
"tcp_established",
85+
"tcp_syn_sent",
86+
"tcp_close"
87+
],
88+
"metrics_collection_interval": 60
89+
},
90+
"processes": {
91+
"measurement": [
92+
"running",
93+
"sleeping",
94+
"dead"
95+
]
96+
}
97+
},
98+
"append_dimensions": {
99+
"ImageId": "${aws:ImageId}",
100+
"InstanceId": "${aws:InstanceId}",
101+
"InstanceType": "${aws:InstanceType}",
102+
"AutoScalingGroupName": "${aws:AutoScalingGroupName}"
103+
},
104+
"aggregation_dimensions" : [["ImageId"], ["InstanceId", "InstanceType"], ["d1"],[]]
105+
},
106+
"logs": {
107+
"logs_collected": {
108+
"files": {
109+
"collect_list": [
110+
{
111+
"file_path": "/home/ec2-user/accountprocessor/logs/accountprocessor-logging-dev.log",
112+
"log_group_name": "accountprocessor",
113+
"log_stream_name": "{instance_id}_{hostname}",
114+
"timezone": "Local"
115+
}
116+
]
117+
}
118+
},
119+
"log_stream_name": "/ec2/catchall"
120+
}
121+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
provider "aws" {
2+
profile = "Admin-Account-Access-489455091964"
3+
region = "eu-central-1"
4+
}
5+
6+
data "aws_ami" "latest-linux" {
7+
most_recent = true
8+
9+
filter {
10+
name = "name"
11+
values = ["*Linux 2023*"]
12+
}
13+
14+
filter {
15+
name = "virtualization-type"
16+
values = ["hvm"]
17+
}
18+
}
19+
20+
resource "aws_key_pair" "tf-key-pair" {
21+
key_name = "tf-key-pair"
22+
public_key = tls_private_key.rsa.public_key_openssh
23+
}
24+
25+
resource "tls_private_key" "rsa" {
26+
algorithm = "RSA"
27+
rsa_bits = 4096
28+
}
29+
30+
resource "local_file" "tf-key" {
31+
content = tls_private_key.rsa.private_key_pem
32+
filename = "tf-key-pair.pem"
33+
}
34+
35+
resource "aws_vpc_security_group_ingress_rule" "sg-ssh" {
36+
security_group_id = aws_security_group.main.id
37+
38+
cidr_ipv4 = "0.0.0.0/0"
39+
from_port = 22
40+
ip_protocol = "tcp"
41+
to_port = 22
42+
}
43+
44+
resource "aws_vpc_security_group_egress_rule" "sg-down" {
45+
security_group_id = aws_security_group.main.id
46+
47+
cidr_ipv4 = "0.0.0.0/0"
48+
from_port = 443
49+
ip_protocol = "tcp"
50+
to_port = 443
51+
}
52+
53+
resource "aws_cloudwatch_log_group" "accountsAppLogs" {
54+
name = "accounts"
55+
56+
tags = {
57+
Environment = "production"
58+
Application = "accountsApi"
59+
}
60+
}
61+
62+
resource "aws_security_group" "main" {
63+
64+
}
65+
66+
resource "aws_instance" "ec2-web1" {
67+
# ami = data.aws_ami.latest-linux.id
68+
ami = "ami-0b7fd829e7758b06d"
69+
instance_type = "t2.micro"
70+
tags = {
71+
Name = "app-ec2-server",
72+
Created_By = "pratik"
73+
}
74+
key_name = "tf-key-pair"
75+
vpc_security_group_ids = [aws_security_group.main.id]
76+
user_data = <<-EOF
77+
#!/bin/bash
78+
echo "installing jdk"
79+
yum update -y
80+
wget https://download.java.net/java/GA/jdk20.0.1/b4887098932d415489976708ad6d1a4b/9/GPL/openjdk-20.0.1_linux-x64_bin.tar.gz
81+
tar xvf openjdk*
82+
export JAVA_HOME=jdk-20.0.1
83+
export PATH=$JAVA_HOME/bin:$PATH
84+
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
85+
sudo yum install amazon-cloudwatch-agent
86+
EOF
87+
}
88+
89+
resource "aws_iam_role" "ec2_role" {
90+
name = "ec2_role"
91+
92+
# Terraform's "jsonencode" function converts a
93+
# Terraform expression result to valid JSON syntax.
94+
assume_role_policy = jsonencode({
95+
"Version": "2012-10-17",
96+
"Statement": [
97+
{
98+
"Effect": "Allow",
99+
"Action": [
100+
"cloudwatch:PutMetricData",
101+
"ec2:DescribeVolumes",
102+
"ec2:DescribeTags",
103+
"logs:PutLogEvents",
104+
"logs:DescribeLogStreams",
105+
"logs:DescribeLogGroups",
106+
"logs:CreateLogStream",
107+
"logs:CreateLogGroup"
108+
],
109+
"Resource": "*"
110+
},
111+
{
112+
"Effect": "Allow",
113+
"Action": [
114+
"ssm:GetParameter"
115+
],
116+
"Resource": "arn:aws:ssm:*:*:parameter/AmazonCloudWatch-*"
117+
}
118+
]
119+
})
120+
121+
tags = {
122+
tag-key = "tag-value"
123+
}
124+
}
125+
output "server_private_ip" {
126+
value = aws_instance.ec2-web1.private_ip
127+
}
128+
output "server_public_ipv4" {
129+
value = aws_instance.ec2-web1.public_ip
130+
}
131+
output "server_id" {
132+
value = aws_instance.ec2-web1.id
133+
}

0 commit comments

Comments
 (0)