-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add lambda * feat: add cron job entry point * feat: deploy to lambda using github actions --------- Co-authored-by: Eyo Chen <eyo.chen@amazingtalker.com>
- Loading branch information
Showing
4 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,61 @@ | ||
name: Let's Deploy Cron Lambda | ||
|
||
on: | ||
workflow_run: | ||
workflows: ["Let's Go CI"] | ||
types: | ||
- completed | ||
branches: [main] | ||
|
||
jobs: | ||
deploy-lambda: | ||
if: | | ||
github.event.workflow_run.conclusion == 'success' && | ||
github.event.workflow_run.head_branch == 'main' | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout Source | ||
uses: actions/checkout@v4 | ||
|
||
- name: Get changed files | ||
id: changed-files | ||
uses: tj-actions/changed-files@v42 | ||
|
||
- name: Check if relevant files changed | ||
id: check-changes | ||
run: | | ||
RELEVANT=false | ||
for file in ${{ steps.changed-files.outputs.all_changed_files }}; do | ||
if [[ $file == cmd/cron/* ]]; then | ||
RELEVANT=true | ||
break | ||
fi | ||
done | ||
if [ "$RELEVANT" = "false" ]; then | ||
echo "No relevant files changed, skipping deployment" | ||
exit 78 # Special exit code that GitHub Actions recognizes as neutral | ||
fi | ||
- name: Setup Go | ||
uses: actions/setup-go@v5 | ||
with: | ||
go-version: '1.22.x' | ||
|
||
- name: Configure AWS Credentials | ||
uses: aws-actions/configure-aws-credentials@v4 | ||
with: | ||
aws-access-key-id: ${{ secrets.AWS_KEY }} | ||
aws-secret-access-key: ${{ secrets.AWS_SECRET }} | ||
aws-region: ${{ secrets.AWS_REGION }} | ||
|
||
- name: Build Lambda Function | ||
run: | | ||
GOOS=linux GOARCH=amd64 go build -o bootstrap cmd/cron/main.go | ||
zip go-lambda.zip bootstrap | ||
- name: Deploy to Lambda | ||
run: | | ||
aws lambda update-function-code \ | ||
--function-name ${{ secrets.AWS_LAMBDA_FUNCTION_NAME }} \ | ||
--zip-file fileb://go-lambda.zip |
This file contains 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,67 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/aws/aws-lambda-go/lambda" | ||
adapter "github.com/eyo-chen/expense-tracker-go/internal/adapter" | ||
"github.com/eyo-chen/expense-tracker-go/internal/usecase/monthlytrans" | ||
"github.com/eyo-chen/expense-tracker-go/pkg/logger" | ||
_ "github.com/go-sql-driver/mysql" | ||
) | ||
|
||
func main() { | ||
lambda.Start(handleRequest) | ||
} | ||
|
||
func handleRequest(ctx context.Context) error { | ||
logger.Register() | ||
|
||
logger.Info("Connecting to database...") | ||
mysqlDB, err := newMysqlDB() | ||
if err != nil { | ||
logger.Error("Unable to connect to mysql database", "error", err) | ||
return err | ||
} | ||
defer mysqlDB.Close() | ||
|
||
// Setup adapter and usecase | ||
adapter := adapter.New(mysqlDB, nil, nil, nil, "") | ||
monthlyTransUC := monthlytrans.New(adapter.MonthlyTrans, adapter.Transaction) | ||
|
||
// Execute monthly transaction aggregation for previous month | ||
previousMonth := time.Now().AddDate(0, -1, 0) | ||
if err := monthlyTransUC.Create(ctx, previousMonth); err != nil { | ||
logger.Error("Failed to create monthly transaction aggregation", "error", err) | ||
return err | ||
} | ||
|
||
logger.Info("Successfully created monthly transaction aggregation", "month", previousMonth.Format(time.DateOnly)) | ||
return nil | ||
} | ||
|
||
func newMysqlDB() (*sql.DB, error) { | ||
config := map[string]string{ | ||
"host": os.Getenv("DB_HOST"), | ||
"port": os.Getenv("DB_PORT"), | ||
"name": os.Getenv("DB_NAME"), | ||
"user": os.Getenv("DB_USER"), | ||
"password": os.Getenv("DB_PASSWORD"), | ||
} | ||
|
||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true", config["user"], config["password"], config["host"], config["port"], config["name"]) | ||
db, err := sql.Open("mysql", dsn) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err = db.Ping(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return db, nil | ||
} |
This file contains 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
This file contains 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