-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/lambda #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 30 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
b5f7f83
feat: adiciona arquivos terraform
nathansouzaa3 8c133c7
feat: adiciona codigo da lambda
nathansouzaa3 4ace87b
feat: adiciona pipeline de deploy
nathansouzaa3 4c3817b
Merge remote-tracking branch 'origin/main' into feature/lambda
nathansouzaa3 f1ee305
fix: remove profile
nathansouzaa3 6913580
feat: ajusta organização das pastas
nathansouzaa3 9bf16c7
fix: ajusta pipeline
nathansouzaa3 5d9f512
feat: adiciona push imagem docker
nathansouzaa3 8e1742c
feat: adiciona create_infra da lambda
nathansouzaa3 9bf9536
fix: ajusta terraform para buscar ecr existente
nathansouzaa3 96b2f6b
feat: adiciona o create infra no pipeline
nathansouzaa3 145395f
fix: arruma nome do script
nathansouzaa3 e6e6160
fix: ajusta poetry no dockerfile
nathansouzaa3 eb21bec
fix: ajusta terraform
nathansouzaa3 aaa990f
feat: adicione bucket s3
nathansouzaa3 15f599a
fix: remove config duplicada
nathansouzaa3 9db68f5
fix: remove variavel do nome do bucket
nathansouzaa3 aee22ac
fix: ajusta criação da iam role
nathansouzaa3 2fff4b2
fix: ajusta comando do terraform
nathansouzaa3 6373e3f
feat: adiciona diretorio dockerfile
nathansouzaa3 4463ae3
fix: cria arquivo push_image lambda
nathansouzaa3 55aba98
fix: ajusta dockerfile
nathansouzaa3 62b6e30
feat: adiciona modelo para teste
nathansouzaa3 a82d441
feat: adiciona pipeline de destroy
nathansouzaa3 f3a999f
fix: ajusta pipeline para rodar manualmente
nathansouzaa3 103991d
teste pipeline destroy
nathansouzaa3 fc274dc
fix: ajusta pipeline para rodar manualmente
nathansouzaa3 41b3b0f
fix: ajusta organização das pastas
nathansouzaa3 d331629
fix: ajusta nome da branch
nathansouzaa3 be6635a
remove modelo do repo
nathansouzaa3 0f1bbdf
remove arquivo tfstate
nathansouzaa3 4336c59
fix: ajusta nome do repo
nathansouzaa3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
name: Pipeline de Deploy Lambda | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
|
||
permissions: | ||
id-token: write | ||
contents: read | ||
|
||
jobs: | ||
deploy: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Check out code | ||
uses: actions/checkout@v4 | ||
with: | ||
ref: ${{ github.ref }} | ||
|
||
- name: Set up Terraform | ||
uses: hashicorp/setup-terraform@v3 | ||
with: | ||
terraform_version: 1.8.0 | ||
|
||
- name: Configure AWS Credentials | ||
uses: aws-actions/configure-aws-credentials@v4 | ||
with: | ||
role-to-assume: "arn:aws:iam::340752815603:role/oidc-github-framework-eml-role" | ||
role-session-name: GitHub_to_AWS_via_FederatedOIDC | ||
aws-region: ${{ env.AWS_REGION }} | ||
|
||
- name: Create ECR | ||
run: | | ||
chmod +x deployment/scripts/create_ecr.sh | ||
./deployment/scripts/create_ecr.sh ${{ env.REPO_NAME }} ${{ env.AWS_REGION }} | ||
|
||
- name: Build and push image | ||
run: | | ||
chmod +x deployment/scripts/push_image_lambda.sh | ||
./deployment/scripts/push_image_lambda.sh ${{ env.REPO_NAME }} ${{ env.AWS_REGION }} | ||
|
||
- name: Create infra and deploy | ||
run: | | ||
chmod +x deployment/scripts/create_infra_lambda.sh | ||
./deployment/scripts/create_infra_lambda.sh ${{ env.AWS_REGION }} | ||
|
||
|
||
env: | ||
REPO_NAME: "prediction-eml" | ||
AWS_REGION: "us-east-1" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
name: Pipeline de Destroy Lambda | ||
|
||
on: | ||
workflow_dispatch: | ||
|
||
permissions: | ||
id-token: write | ||
contents: read | ||
|
||
jobs: | ||
destroy: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
with: | ||
ref: ${{ github.ref }} | ||
|
||
- name: Configure AWS Credentials | ||
uses: aws-actions/configure-aws-credentials@v4 | ||
with: | ||
role-to-assume: "arn:aws:iam::340752815603:role/oidc-github-framework-eml-role" | ||
role-session-name: GitHub_to_AWS_via_FederatedOIDC | ||
aws-region: ${{ env.AWS_REGION }} | ||
|
||
- name: Setup Terraform | ||
uses: hashicorp/setup-terraform@v3 | ||
with: | ||
terraform_version: 1.8.0 | ||
|
||
- name: Destroy infrastructure | ||
run: | | ||
chmod +x deployment/scripts/destroy_infra_lambda.sh | ||
./deployment/scripts/destroy_infra_lambda.sh ${{ env.AWS_REGION }} | ||
|
||
env: | ||
REPO_NAME: "prediction-eml" | ||
nathansouzaa3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
AWS_REGION: "us-east-1" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import json | ||
nathansouzaa3 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import pandas as pd | ||
from src.pipelines.predict import load_model, make_predictions | ||
|
||
|
||
MODEL_FILE = "artifacts/models/model.joblib" | ||
model = load_model(MODEL_FILE) | ||
|
||
|
||
def validate_input(item): | ||
"""Valida se o item contém pelo menos um valor e é uma lista com o tamanho correto.""" | ||
if not item or not isinstance(item, list) or len(item) != 4: | ||
raise ValueError("O item deve ser uma lista com 4 valores.") | ||
|
||
|
||
def make_prediction(item): | ||
"""Faz previsão com base nos dados de entrada.""" | ||
data = pd.DataFrame( | ||
[item], columns=["sepal_length", "sepal_width", "petal_length", "petal_width"] | ||
) | ||
prediction = make_predictions(model, data) | ||
return prediction.astype(int).tolist()[0] | ||
|
||
|
||
def process_event(event): | ||
"""Processa o evento e gera previsões.""" | ||
if isinstance(event, str): | ||
event = json.loads(event) | ||
if "body" in event: | ||
body = json.loads(event["body"]) | ||
else: | ||
body = event | ||
predictions = [] | ||
|
||
for item in body: | ||
try: | ||
validate_input(item) | ||
prediction = make_prediction(item) | ||
predictions.append({"prediction": prediction}) | ||
except Exception as e: | ||
predictions.append({"error": str(e)}) | ||
return predictions | ||
|
||
|
||
def lambda_handler(event, context): | ||
"""Função principal que recebe o evento e contexto.""" | ||
predictions = process_event(event) | ||
return {"statusCode": 200, "body": json.dumps(predictions)} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
FROM public.ecr.aws/lambda/python:3.10 | ||
|
||
# Instalar o Poetry | ||
RUN pip install poetry | ||
|
||
# Copiar arquivos de configuração do Poetry | ||
COPY pyproject.toml poetry.lock* ./ | ||
|
||
# Configurar o Poetry para não criar virtualenv | ||
RUN poetry config virtualenvs.create false | ||
|
||
# Instalar as dependências do projeto | ||
RUN poetry install --no-dev | ||
|
||
# Copiar os artefatos e o código-fonte | ||
COPY artifacts/ artifacts/ | ||
COPY api/lambda/app/lambda_function.py ./ | ||
COPY src/ src/ | ||
COPY config/ config/ | ||
|
||
# Comando padrão para executar a função Lambda | ||
CMD ["lambda_function.lambda_handler"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
resource "aws_api_gateway_rest_api" "api" { | ||
name = "PredictionAPI" | ||
description = "API para a função Lambda de previsão" | ||
} | ||
|
||
resource "aws_api_gateway_resource" "predict" { | ||
rest_api_id = aws_api_gateway_rest_api.api.id | ||
parent_id = aws_api_gateway_rest_api.api.root_resource_id | ||
path_part = "predict" | ||
} | ||
|
||
resource "aws_api_gateway_method" "predict_method" { | ||
rest_api_id = aws_api_gateway_rest_api.api.id | ||
resource_id = aws_api_gateway_resource.predict.id | ||
http_method = "POST" | ||
authorization = "NONE" | ||
} | ||
|
||
resource "aws_api_gateway_integration" "predict_integration" { | ||
rest_api_id = aws_api_gateway_rest_api.api.id | ||
resource_id = aws_api_gateway_resource.predict.id | ||
http_method = aws_api_gateway_method.predict_method.http_method | ||
integration_http_method = "POST" | ||
type = "AWS_PROXY" | ||
uri = "arn:aws:apigateway:${var.region}:lambda:path/2015-03-31/functions/${aws_lambda_function.prediction_eml_lambda.arn}/invocations" | ||
} | ||
|
||
resource "aws_api_gateway_deployment" "api_deployment" { | ||
depends_on = [aws_api_gateway_integration.predict_integration] | ||
rest_api_id = aws_api_gateway_rest_api.api.id | ||
stage_name = "prod" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
resource "aws_iam_role" "prediction_eml_role" { | ||
name = "prediction_eml_role" | ||
|
||
assume_role_policy = jsonencode({ | ||
Version = "2012-10-17" | ||
Statement = [ | ||
{ | ||
Action = "sts:AssumeRole" | ||
Principal = { | ||
Service = "lambda.amazonaws.com" | ||
} | ||
Effect = "Allow" | ||
Sid = "" | ||
}, | ||
] | ||
}) | ||
} | ||
|
||
resource "aws_iam_policy_attachment" "prediction_eml_policy_attachment" { | ||
name = "prediction_eml_policy_attachment" | ||
roles = [aws_iam_role.prediction_eml_role.name] | ||
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
terraform { | ||
backend "s3" { | ||
bucket = var.bucket_name | ||
key = "terraform/terraform.tfstate" | ||
region = "us-east-1" | ||
encrypt = true | ||
} | ||
} | ||
|
||
resource "aws_lambda_function" "prediction_eml_lambda" { | ||
function_name = "prediction_eml" | ||
role = aws_iam_role.prediction_eml_role.arn | ||
package_type = "Image" | ||
image_uri = "${data.aws_caller_identity.current.account_id}.dkr.ecr.${var.region}.amazonaws.com/${var.repository_name}:latest" | ||
|
||
memory_size = 256 | ||
timeout = 120 | ||
} | ||
|
||
resource "aws_lambda_permission" "allow_api_gateway" { | ||
statement_id = "AllowAPIGatewayInvoke" | ||
action = "lambda:InvokeFunction" | ||
function_name = aws_lambda_function.prediction_eml_lambda.function_name | ||
principal = "apigateway.amazonaws.com" | ||
source_arn = "${aws_api_gateway_rest_api.api.execution_arn}/*/*" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
output "api_url" { | ||
value = "https://${aws_api_gateway_rest_api.api.id}.execute-api.${var.region}.amazonaws.com/prod/predict" | ||
description = "URL do endpoint da API" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
provider "aws" { | ||
region = var.region | ||
} | ||
|
||
data "aws_caller_identity" "current" {} |
nathansouzaa3 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"version": 4, | ||
"terraform_version": "1.9.4", | ||
"serial": 401, | ||
"lineage": "79795974-11da-3e4d-08c5-b20287c1a486", | ||
"outputs": {}, | ||
"resources": [], | ||
"check_results": null | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
variable "repository_name" { | ||
description = "Nome do repositório ECR" | ||
type = string | ||
default = "prediction-eml" | ||
} | ||
|
||
variable "region" { | ||
description = "A região onde a infraestrutura será criada." | ||
default = "us-east-1" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text) | ||
BUCKET_NAME="$ACCOUNT_ID-prediction-eml" | ||
AWS_REGION=$1 | ||
|
||
# Verifica se o bucket existe | ||
if aws s3api head-bucket --bucket "$BUCKET_NAME" 2>/dev/null; then | ||
echo "O bucket '$BUCKET_NAME' já existe." | ||
else | ||
# Se o bucket não existir, cria o bucket | ||
aws s3api create-bucket --bucket "$BUCKET_NAME" --region us-east-1 | ||
echo "Bucket '$BUCKET_NAME' criado com sucesso." | ||
fi | ||
|
||
terraform -chdir="deployment/infrastructure/terraform-lambda" init -backend-config="bucket=$BUCKET_NAME" -backend-config="region=$AWS_REGION" | ||
terraform -chdir="deployment/infrastructure/terraform-lambda" apply -auto-approve |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text) | ||
BUCKET_NAME="$ACCOUNT_ID-prediction-eml" | ||
AWS_REGION=$1 | ||
|
||
# Verifica se o bucket existe | ||
if aws s3api head-bucket --bucket "$BUCKET_NAME" 2>/dev/null; then | ||
echo "O bucket '$BUCKET_NAME' já existe." | ||
else | ||
# Se o bucket não existir, cria o bucket | ||
aws s3api create-bucket --bucket "$BUCKET_NAME" --region us-east-1 | ||
echo "Bucket '$BUCKET_NAME' criado com sucesso." | ||
fi | ||
|
||
# Inicializa o Terraform | ||
terraform -chdir="deployment/infrastructure/terraform-lambda" init -backend-config="bucket=$BUCKET_NAME" -backend-config="region=$AWS_REGION" | ||
|
||
# Comando para destruir a infraestrutura | ||
terraform -chdir="deployment/infrastructure/terraform-lambda" destroy -auto-approve |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
DOCKER_IMAGE_NAME=$1 | ||
AWS_REGION=$2 | ||
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text) | ||
REPO_URI="$ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$DOCKER_IMAGE_NAME" | ||
|
||
docker build -t $DOCKER_IMAGE_NAME -f deployment/docker/Dockerfile.lambda . | ||
docker tag "$DOCKER_IMAGE_NAME:latest" "$REPO_URI:latest" | ||
aws ecr get-login-password --region "$AWS_REGION" | docker login --username AWS --password-stdin "$REPO_URI" | ||
docker push "$REPO_URI:latest" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.