Skip to content

Commit

Permalink
upload files to s3 and reduce the memory and increase timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
tajewole-moj committed Nov 9, 2023
1 parent 55e9ebc commit f1ff91b
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 5 deletions.
2 changes: 1 addition & 1 deletion terraform/environments/apex/event_triggers.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
resource "aws_cloudwatch_event_rule" "snapshotDBFunctionmon_sun" {
name = "laa-createSnapshotRule-${local.application_name}-${local.environment}-mp"
description = "Daily snapshots of Oracle volumes"
schedule_expression = "cron(51 14 ? * MON-SUN *)"
schedule_expression = "cron(39 11 ? * MON-SUN *)"
}
resource "aws_lambda_permission" "allow_cloudwatch_to_call_check_mon_sun" {
statement_id = "AllowExecutionFromCloudWatch"
Expand Down
13 changes: 9 additions & 4 deletions terraform/environments/apex/lambda.tf
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ resource "aws_lambda_layer_version" "lambda_layer" {


resource "aws_lambda_function" "snapshotDBFunction" {
description = "Snapshot volumes for Oracle EC2"
function_name = local.snapshotDBFunctionname
role = module.iambackup.backuprole
handler = local.snapshotDBFunctionhandler
Expand All @@ -72,8 +73,8 @@ resource "aws_lambda_function" "snapshotDBFunction" {
layers = [aws_lambda_layer_version.lambda_layer.arn]
s3_bucket = module.s3_bucket_lambda.lambdabucketname
s3_key = local.snapshotDBFunctionfilename
memory_size = 350
timeout = 300
memory_size = 128
timeout = 900


environment {
Expand All @@ -93,13 +94,16 @@ resource "aws_lambda_function" "snapshotDBFunction" {
}

resource "aws_lambda_function" "deletesnapshotFunction" {
description = "Clean up script to delete old unused snapshots"
function_name = local.deletesnapshotFunctionname
role = module.iambackup.backuprole
handler = local.deletesnapshotFunctionhandler
source_code_hash = data.archive_file.deletesnapshot_file.output_base64sha256
runtime = local.deletesnapshotFunctionruntime
s3_bucket = module.s3_bucket_lambda.lambdabucketname
s3_key = local.deletesnapshotFunctionfilename
memory_size = 1024
timeout = 900

environment {
variables = {
Expand All @@ -119,6 +123,7 @@ resource "aws_lambda_function" "deletesnapshotFunction" {


resource "aws_lambda_function" "connectDBFunction" {
description = "SSH to the DB EC2"
function_name = local.connectDBFunctionname
role = module.iambackup.backuprole
handler = local.connectDBFunctionhandler
Expand All @@ -127,8 +132,8 @@ resource "aws_lambda_function" "connectDBFunction" {
layers = [aws_lambda_layer_version.lambda_layer.arn]
s3_bucket = module.s3_bucket_lambda.lambdabucketname
s3_key = local.connectDBFunctionfilename
memory_size = 350
timeout = 300
memory_size = 128
timeout = 900

environment {
variables = {
Expand Down
12 changes: 12 additions & 0 deletions terraform/environments/apex/modules/s3/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,18 @@ bucket = var.bucket_name
tags = var.tags
}


resource "aws_s3_object" "object_name" {
bucket = aws_s3_bucket.laa-lambda-backup.id
for_each = fileset("./zipfiles/", "**")
key = each.value
source = "./zipfiles/${each.value}"
content_type = each.value
}




resource "aws_s3_bucket_ownership_controls" "default" {
bucket = aws_s3_bucket.laa-lambda-backup.id
rule {
Expand Down
36 changes: 36 additions & 0 deletions terraform/environments/apex/zipfiles/createAMI.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

import boto3

# Specify the AWS profile you want to use
aws_profile = 'laa-development-lz'
snapshot_id = 'snap-0331f5921109f4666'
image_name = 'eric-db-srv-RHEL-7.7-280923'



# Initialize the Boto3 EC2 client with the specified profile
session = boto3.Session(profile_name=aws_profile)
ec2_client = session.client('ec2')

# Create an image from the snapshot
response = ec2_client.register_image(
BlockDeviceMappings=[
{
'DeviceName': '/dev/sda1', # Modify as needed
'Ebs': {
'SnapshotId': snapshot_id,
'VolumeSize': 80, # Modify as needed
'VolumeType': 'gp2', # Modify as needed
},
},
],
RootDeviceName='/dev/sda1',
VirtualizationType='hvm',
Name=image_name,
Description='Image created from snapshot in shutdown state',
Architecture= 'x86_64'

)

# Print the newly created image ID
print(f"Image ID: {response['ImageId']}")
Binary file not shown.
Binary file not shown.
Binary file added terraform/environments/apex/zipfiles/nodejs.zip
Binary file not shown.

0 comments on commit f1ff91b

Please sign in to comment.