From 206284e599e48b13be5c1431393f76a7edaf811c Mon Sep 17 00:00:00 2001 From: Joseph Manley Date: Thu, 13 Aug 2020 01:06:25 -0400 Subject: [PATCH] Intial setup --- .github/workflows/ship.yml | 37 +++++++++++++++++++++++++++++ create_issue/lambda_function.py | 19 +++++++++++++++ create_issue/requirements.txt | 2 ++ top.yaml | 41 +++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 .github/workflows/ship.yml create mode 100644 create_issue/lambda_function.py create mode 100644 create_issue/requirements.txt create mode 100644 top.yaml diff --git a/.github/workflows/ship.yml b/.github/workflows/ship.yml new file mode 100644 index 0000000..40592ec --- /dev/null +++ b/.github/workflows/ship.yml @@ -0,0 +1,37 @@ + +name: Build & Ship + +on: + push: + branches: + - master + +jobs: + deploy: + runs-on: ubuntu-latest + steps: + - name: Checkout Repo + uses: actions/checkout@v1 + - name: Build Lambdas + run: | + file="create_issue" + echo "Building: $file" + cd $file + echo "Installing requirements..." + pip install -q -r requirements.txt -t . + echo "Zipping..." + zip -qq -r ../$file.zip . + cd .. + echo "Cleaning up..." + rm -rf $file + - name: Ship to S3 + uses: jakejarvis/s3-sync-action@master + with: + args: --follow-symlinks --delete + env: + SOURCE_DIR: "./" + AWS_REGION: "us-east-1" + DEST_DIR: github-issues + AWS_S3_BUCKET: sumu-stacks + AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} + AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} \ No newline at end of file diff --git a/create_issue/lambda_function.py b/create_issue/lambda_function.py new file mode 100644 index 0000000..cc69bc0 --- /dev/null +++ b/create_issue/lambda_function.py @@ -0,0 +1,19 @@ +import logging, boto3, os +from github import Github + +logger = logging.getLogger() +logger.setLevel(logging.INFO) + +github = Github("") + +def lambda_handler(event, context): + + repo = github.get_repo(os.environ.get("REPOSITORY")) + + if not "Records" in event: + raise Exception("Missing key Records in event!") + + for record in event["Records"]: + if "Sns" in record: + sns_event = record["Sns"] + repo.create_issue(sns_event["Subject"], body=sns_event["Message"] + "\nThis was generated by " + os.environ.get("GENERATED_OWNER", "AWS")) \ No newline at end of file diff --git a/create_issue/requirements.txt b/create_issue/requirements.txt new file mode 100644 index 0000000..e7129d5 --- /dev/null +++ b/create_issue/requirements.txt @@ -0,0 +1,2 @@ +boto3 +PyGithub \ No newline at end of file diff --git a/top.yaml b/top.yaml new file mode 100644 index 0000000..f03b453 --- /dev/null +++ b/top.yaml @@ -0,0 +1,41 @@ +AWSTemplateFormatVersion: '2010-09-09' +Description: SNS Topic to create GitHub Issues +Parameters: + TopicName: + Type: String + GithubRepository: + Type: String + GenerateOwnerName: + Type: String + +Resources: + SnsTopic: + Type: AWS::SNS::Topic + Properties: + TopicName: !Ref TopicName + + CreateIssueLambda: + Type: AWS::Lambda::Function + Properties: + Handler: lambda_function.lambda_handler + Runtime: python3.8 + Code: + S3Bucket: "sumu-stacks" + S3Key: !Sub "github-issues/create_issue.zip" + FunctionName: "Github-CreateIssue" + Description: Lambda that creates a issue in Github + MemorySize: 128 + Timeout: 10 + #Role: !GetAtt AlertExecutionerRole.Arn + Environment: + Variables: + GENERATED_OWNER: !Ref GenerateOwnerName + REPOSITORY: !Ref GithubRepository + + SnsLambdaPermission: + Type: AWS::Lambda::Permission + Properties: + Action: lambda:InvokeFunction + Principal: sns.amazonaws.com + SourceArn: !Ref SnsTopic + FunctionName: !GetAtt AlertHandler.Arn