diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 19e635b..5ffd0fb 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -23,10 +23,10 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v3 + uses: actions/checkout@v6 - name: Setup python - uses: actions/setup-python@v4 + uses: actions/setup-python@v6 with: python-version: ${{ matrix.python-version }} @@ -37,4 +37,92 @@ jobs: run: pytest - name: Run linter - run: flake8 . \ No newline at end of file + run: flake8 . + + deploy: + runs-on: ubuntu-latest + + defaults: + run: + working-directory: 01-ci-lab + + permissions: + id-token: write + + needs: test + + env: + LAMBDA_NAME: calculator-app + + steps: + + - name: Checkout repository + uses: actions/checkout@v6 + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v6 + with: + role-to-assume: arn:aws:iam::350469506175:role/github-actions-lambda-deploy-role + aws-region: eu-central-1 + + - name: Prepare Lambda package + run: | + cp app/calculator.py lambda_function.py + zip function.zip lambda_function.py + + - name: Create/update Lambda + run: | + if aws lambda get-function --function-name ${{ env.LAMBDA_NAME }} >/dev/null 2>&1; then + + echo "${{ env.LAMBDA_NAME }} lambda exists" + + aws lambda update-function-code \ + --function-name ${{ env.LAMBDA_NAME }} \ + --zip-file fileb://function.zip + + else + + echo "${{ env.LAMBDA_NAME }} lambda doesn't exist, creating" + + aws lambda create-function \ + --function-name ${{ env.LAMBDA_NAME }} \ + --role arn:aws:iam::350469506175:role/service-role/calculator-app-role-cnpkrehb \ + --runtime python3.13 \ + --handler lambda_function.lambda_handler \ + --zip-file fileb://function.zip + + fi + + - name: Create function URL + run: | + if ! aws lambda get-function-url-config \ + --function-name ${{ env.LAMBDA_NAME }} >/dev/null 2>&1; then + + echo "URL doesn't exist, creating" + + aws lambda create-function-url-config \ + --function-name ${{ env.LAMBDA_NAME }} \ + --auth-type NONE + + aws lambda add-permission \ + --function-name ${{ env.LAMBDA_NAME }} \ + --statement-id FunctionAllowPublicAccessUrl \ + --action lambda:InvokeFunctionUrl \ + --principal "*" \ + --function-url-auth-type NONE + + aws lambda add-permission \ + --function-name ${{ env.LAMBDA_NAME }} \ + --statement-id FunctionAllowPublicAccess \ + --action lambda:InvokeFunction \ + --principal "*" \ + --invoked-via-function-url + + else + + echo "URL exists" + + aws lambda get-function-url-config \ + --function-name ${{ env.LAMBDA_NAME }} + + fi \ No newline at end of file diff --git a/01-ci-lab/app/calculator.py b/01-ci-lab/app/calculator.py index 98c312c..092f1e6 100644 --- a/01-ci-lab/app/calculator.py +++ b/01-ci-lab/app/calculator.py @@ -1,6 +1,58 @@ +import json + + def add(a, b): return a + b -def divide(a, b): # +def divide(a, b): return a / b + + +def multiply(a, b): + return a * b + + +def substract(a, b): + return a - b + + +def power(a, b): + return a**b + + +def lambda_handler(event, context): + # Function URL / API Gateway + if "body" in event: + if isinstance(event["body"], str): + body = json.loads(event["body"]) + else: + body = event["body"] + # Test from AWS Console + else: + body = event + + a = float(body["a"]) + b = float(body["b"]) + operation = body["operation"] + + if operation == "add": + result = add(a, b) + elif operation == "divide": + result = divide(a, b) + elif operation == "multiply": + result = multiply(a, b) + elif operation == "substract": + result = substract(a, b) + elif operation == "power": + result = power(a, b) + else: + return { + "statusCode": 400, + "body": json.dumps({"error": "Unsupported operation"}) + } + + return { + "statusCode": 200, + "body": json.dumps({"result": result}) + }