Terraform - Lambda - agent ec2 stop 추가

This commit is contained in:
dsk-minchulahn
2024-01-29 14:20:01 +09:00
parent 61149888de
commit 3c9be964a5
9 changed files with 149 additions and 0 deletions

View File

@@ -0,0 +1,44 @@
# This file is maintained automatically by "terraform init".
# Manual edits may be lost in future updates.
provider "registry.terraform.io/hashicorp/archive" {
version = "2.4.2"
hashes = [
"h1:G4v6F6Lhqlo3EKGBKEK/kJRhNcQiRrhEdUiVpBHKHOA=",
"zh:08faed7c9f42d82bc3d406d0d9d4971e2d1c2d34eae268ad211b8aca57b7f758",
"zh:3564112ed2d097d7e0672378044a69b06642c326f6f1584d81c7cdd32ebf3a08",
"zh:53cd9afd223c15828c1916e68cb728d2be1cbccb9545568d6c2b122d0bac5102",
"zh:5ae4e41e3a1ce9d40b6458218a85bbde44f21723943982bca4a3b8bb7c103670",
"zh:5b65499218b315b96e95c5d3463ea6d7c66245b59461217c99eaa1611891cd2c",
"zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3",
"zh:7f45b35a8330bebd184c2545a41782ff58240ed6ba947274d9881dd5da44b02e",
"zh:87e67891033214e55cfead1391d68e6a3bf37993b7607753237e82aa3250bb71",
"zh:de3590d14037ad81fc5cedf7cfa44614a92452d7b39676289b704a962050bc5e",
"zh:e7e6f2ea567f2dbb3baa81c6203be69f9cd6aeeb01204fd93e3cf181e099b610",
"zh:fd24d03c89a7702628c2e5a3c732c0dede56fa75a08da4a1efe17b5f881c88e2",
"zh:febf4b7b5f3ff2adff0573ef6361f09b6638105111644bdebc0e4f575373935f",
]
}
provider "registry.terraform.io/hashicorp/aws" {
version = "5.34.0"
constraints = "~> 5.0"
hashes = [
"h1:Tbq6dKE+XyXmkup6+7eQj2vH+eCJipk8R3VXhebVYi4=",
"zh:01bb20ae12b8c66f0cacec4f417a5d6741f018009f3a66077008e67cce127aa4",
"zh:3b0c9bdbbf846beef2c9573fc27898ceb71b69cf9d2f4b1dd2d0c2b539eab114",
"zh:5226ecb9c21c2f6fbf1d662ac82459ffcd4ad058a9ea9c6200750a21a80ca009",
"zh:6021b905d9b3cd3d7892eb04d405c6fa20112718de1d6ef7b9f1db0b0c97721a",
"zh:9b12af85486a96aedd8d7984b0ff811a4b42e3d88dad1a3fb4c0b580d04fa425",
"zh:9e61b8e0ccf923979cd2dc1f1140dbcb02f92248578e10c1996f560b6306317c",
"zh:ad6bf62cdcf531f2f92f6416822918b7ba2af298e4a0065c6baf44991fda982d",
"zh:b698b041ef38837753bbe5265dddbc70b76e8b8b34c5c10876e6aab0eb5eaf63",
"zh:bb799843c534f6a3f072a99d93a3b53ff97c58a96742be15518adf8127706784",
"zh:cebee0d942c37cd3b21e9050457cceb26d0a6ea886b855dab64bb67d78f863d1",
"zh:e061fdd1cb99e7c81fb4485b41ae000c6792d38f73f9f50aed0d3d5c2ce6dcfb",
"zh:eeb4943f82734946362696928336357cd1d36164907ae5905da0316a67e275e1",
"zh:ef09b6ad475efa9300327a30cbbe4373d817261c8e41e5b7391750b16ef4547d",
"zh:f01aab3881cd90b3f56da7c2a75f83da37fd03cc615fc5600a44056a7e0f9af7",
"zh:fcd0f724ebc4b56a499eb6c0fc602de609af18a0d578befa2f7a8df155c55550",
]
}

View File

@@ -0,0 +1,18 @@
resource "aws_cloudwatch_event_rule" "stop_lambda" {
name = var.stop_lambda_function_name
description = "Schedule for Stop Lambda Function"
schedule_expression = var.stop_lambda_schedule
}
resource "aws_cloudwatch_event_target" "stop_lambda" {
rule = aws_cloudwatch_event_rule.stop_lambda.name
target_id = var.stop_lambda_function_name
arn = aws_lambda_function.stop_lambda_function.arn
}
resource "aws_lambda_permission" "allow_cloudwatch_stop_lambda" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.stop_lambda_function.function_name
principal = "events.amazonaws.com"
}

View File

@@ -0,0 +1,8 @@
import boto3
region = 'ap-northeast-2'
instances = ['i-0b8d61b56a3baf918']
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
ec2.start_instances(InstanceIds=instances)
print('Started instances: ' + str(instances))

View File

@@ -0,0 +1,8 @@
import boto3
region = 'ap-northeast-2'
instances = ['i-0b8d61b56a3baf918']
ec2 = boto3.client('ec2', region_name=region)
def lambda_handler(event, context):
ec2.stop_instances(InstanceIds=instances)
print('Stopped instances: ' + str(instances))

View File

@@ -0,0 +1,43 @@
provider "aws" {
region = var.aws_region
}
data "archive_file" "start_lambda_function" {
type = "zip"
source_file = "./lambda_function/${var.start_lambda_function_name}.py"
output_path = "./lambda_function/${var.start_lambda_function_name}.zip"
}
data "archive_file" "stop_lambda_function" {
type = "zip"
source_file = "./lambda_function/${var.stop_lambda_function_name}.py"
output_path = "./lambda_function/${var.stop_lambda_function_name}.zip"
}
resource "aws_lambda_function" "start_lambda_function" {
filename = "./lambda_function/${var.start_lambda_function_name}.zip"
function_name = var.start_lambda_function_name
role = var.dsk_lambda_role
handler = "${var.start_lambda_function_name}.lambda_handler"
source_code_hash = data.archive_file.start_lambda_function.output_base64sha256
runtime = "python3.9"
}
resource "aws_lambda_function" "stop_lambda_function" {
filename = "./lambda_function/${var.stop_lambda_function_name}.zip"
function_name = var.stop_lambda_function_name
role = var.dsk_lambda_role
handler = "${var.stop_lambda_function_name}.lambda_handler"
source_code_hash = data.archive_file.stop_lambda_function.output_base64sha256
runtime = "python3.9"
}
resource "aws_cloudwatch_log_group" "start_lambda_function_log_group" {
name = "/aws/lambda/${var.start_lambda_function_name}"
retention_in_days = 7
}
resource "aws_cloudwatch_log_group" "stop_lambda_function_log_group" {
name = "/aws/lambda/${var.stop_lambda_function_name}"
retention_in_days = 7
}

View File

@@ -0,0 +1,20 @@
variable "aws_region" {
default = "ap-northeast-2"
}
variable "dsk_lambda_role" {
type = string
default = "arn:aws:iam::508259851457:role/DSK_Lambda_Role"
}
variable "start_lambda_function_name" {
default = "dsk-agent-ec2-start"
}
variable "stop_lambda_function_name" {
default = "dsk-agent-ec2-stop"
}
variable "stop_lambda_schedule" {
default = "cron(0 14 * * ? *)"
}

View File

@@ -0,0 +1,8 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}