Package: cdk-gradle-uploader
Gradle UploaderThis CDK construct checks for new releases of theGradlebuild software.The new release will be made available as copy in an S3 bucket. An information about
the new release can be sent via e-mail or via Slack.Internally the construct usesanS3bucket for storing the Gradle softwareaLambdafunction and one Lambda layer tocheck for the latest Gradle releaseupload if required and notify users viaSNSand e-Mail or alternatively SlackaCloudwatchevent rule to trigger the Lambda functionSetup of the componentsThe S3 BucketBy default, public access to the S3 bucket is disabled. Only the access from a specific IP address (the one I got from my ISP) is allowed and ensured viabucket policies.constbucket=newBucket(this,"bucket",{blockPublicAccess:BlockPublicAccess.BLOCK_ALL,encryption:BucketEncryption.S3_MANAGED,publicReadAccess:false,versioned:false,removalPolicy:RemovalPolicy.DESTROY,});constbucketContentStatement=newPolicyStatement({effect:Effect.ALLOW,actions:["s3:GetObject"],resources:[bucket.bucketArn+"/*"],principals:[newAnyPrincipal()],});bucketContentStatement.addCondition("IpAddress",{"aws:SourceIp":"87.122.220.125/32",});constbucketStatement:PolicyStatement=newPolicyStatement({effect:Effect.ALLOW,actions:["s3:ListBucket","s3:GetBucketLocation"],resources:[bucket.bucketArn],principals:[newAnyPrincipal()],});bucketStatement.addCondition("IpAddress",{"aws:SourceIp":"87.122.220.125/32",});constbucketPolicy=newBucketPolicy(this,"bucketPolicy",{bucket:bucket,});The Lambda functionThe Lambda function is written in Python (version 3.8). The execution time is limited to five minutes and the memory consumption to 512 MByte. Additionally the function gets read/ write access to the S3 bucket and has a log retention period is set to one week.constfn=newFunction(this,"fnUpload",{runtime:Runtime.PYTHON_3_8,description:"Download Gradle distribution to S3 bucket",handler:"gradleUploader.main",code:Code.fromAsset("./lambda/"),timeout:Duration.minutes(5),memorySize:512,logRetention:RetentionDays.ONE_WEEK,layers:[layer],environment:{BUCKET_NAME:bucket.bucketName,TOPIC_ARN:topic.topicArn,},});bucket.grantReadWrite(fn);If Slack is selected as notification channel, then also theWEBHOOK_URLis part of theenvironment.In the additional layer modules like boto3 are included.constlayer=newLayerVersion(this,"GradleUploaderLayer",{code:Code.fromAsset(path.join(__dirname,"../layer-code")),compatibleRuntimes:[Runtime.PYTHON_3_8],license:"Apache-2.0",description:"A layer containing dependencies for thr Gradle Uploader",});The Cloudwatch event ruleEvery first of a month the Lambda functionfnwill be triggered automatically. That seems to be a reasonable period for the update check.consttarget=newLambdaFunction(fn);newRule(this,"ScheduleRule",{schedule:Schedule.cron({minute:"0",hour:"0",day:"1",month:"*"}),targets:[target],});Notifying about new releasesWhenever the release of a new Gradle version is detected, the stack will sent an e-mail to the list of subscriber using SNS.privateaddSubscribers(topic:Topic,subscribers:Array<string>){for(varsubscriberofsubscribers){topic.addSubscription(newEmailSubscription(subscriber));}}The forwarding of information to aSlackchannel is done from within the Lambda function.Testing the Python codedockerrun--rm-v"$PWD":/var/task:ro,delegated-v/home/stefan/Private/programmieren/aws/cdk/gradle_uploader/layer-code:/opt:ro,delegated-eAWS_ACCESS_KEY_ID=XXXXXXXXXX-eAWS_SECRET_ACCESS_KEY=XXXXXXXXXXlambci/lambda:python3.8gradleUploader.mainHow to use the construct in a stackHere is an example how to use the construct:exportclassGradleUploaderStackextendsStack{constructor(scope:Construct,id:string){super(scope,id);newGradleUploader(this,'TestStack',{mailProperties:{subscribers:['<e-mail address>']},slackProperties:{webhook:'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX',},whitelist:['CIDR_1','CIDR_2'],});}}constapp=newApp();newGradleUploaderStack(app,'TestApp');app.synth();LinksAWS Cloud Development KitGradle Homepageboto3Slack