From 733cee9e4f458cd4f8f8ac5b57c3123e4d754c7c Mon Sep 17 00:00:00 2001 From: David Christian Date: Tue, 3 Aug 2021 17:51:33 -0700 Subject: [PATCH] Updated Preso --- 02 - Working with EC2.ps1 | 5 ++-- 03 - S3.ps1 | 32 +++++++++++++++++++++---- 04 - CloudWatch.ps1 | 7 +++--- 05 - Lambda.ps1 | 6 ++--- 06 - CICD.ps1 | 31 ++++++++++++++++++++++++ BasicLambda/basic.ps1 | 3 ++- HelperFunctions/Get-CWLLastLogEvent.ps1 | 9 +++---- LambdaTemplateExample/basic/basic.ps1 | 16 +++++++++++++ LambdaTemplateExample/basic/readme.txt | 15 ++++++++++++ LambdaTemplateExample/buildspec.yaml | 9 +++++++ 10 files changed, 115 insertions(+), 18 deletions(-) create mode 100644 06 - CICD.ps1 create mode 100644 LambdaTemplateExample/basic/basic.ps1 create mode 100644 LambdaTemplateExample/basic/readme.txt create mode 100644 LambdaTemplateExample/buildspec.yaml diff --git a/02 - Working with EC2.ps1 b/02 - Working with EC2.ps1 index 10a0a54..8b37072 100644 --- a/02 - Working with EC2.ps1 +++ b/02 - Working with EC2.ps1 @@ -81,9 +81,10 @@ Get-SSMLatestEC2Image -ImageName 'Windows_Server-2019-English-Full-Base' -Path ' (Get-Command New-EC2Instance).Parameters.Count # Create Multiple Instances -1..5 | % { +1..5 | ForEach-Object { Get-SSMLatestEC2Image -ImageName 'Windows_Server-2019-English-Full-Base' -Path 'ami-windows-latest' | - New-EC2Instance -InstanceType t2.large -SecurityGroupId $groupID -KeyName $keyName -UserData $userData -EncodeUserData -Verbose } + New-EC2Instance -InstanceType t2.large -SecurityGroupId $groupID -KeyName $keyName -UserData $userData -EncodeUserData -Verbose + } # Getting the password diff --git a/03 - S3.ps1 b/03 - S3.ps1 index c31e591..4c6e62a 100644 --- a/03 - S3.ps1 +++ b/03 - S3.ps1 @@ -1,18 +1,40 @@ # Get Buckets Get-S3Bucket + + # New Bucket -New-S3Bucket -BucketName chradavi-s3-imagebucket-test -Verbose +$bucketName = "chradavi-s3-imagebucket-test" +New-S3Bucket -BucketName $bucketName -Verbose + +# Check Encryption +Get-S3Bucket -BucketName $bucketName | + Get-S3BucketEncryption + +# Setup Encryption +$Encryptionconfig = @{ + ServerSideEncryptionByDefault = + @{ServerSideEncryptionAlgorithm = "AES256" + }} + +Set-S3BucketEncryption -BucketName $bucketName -ServerSideEncryptionConfiguration_ServerSideEncryptionRule $Encryptionconfig + + +Get-S3Bucket -BucketName $bucketName | + Get-S3BucketEncryption # Delete All Objects in a bucket -Get-S3Object -BucketName chradavi-s3-imagebucket-test | +Get-S3Object -BucketName $bucketName | Remove-S3Object -Verbose -Force # Upload Objects to a bucket -Get-Item -Path .\Pics\* | +Get-Item -Path C:\github\PowerShellAndAWS\Pics\* | ForEach-Object { $key = [io.path]::Combine("input",$PSItem.Name) - Write-S3Object -BucketName chradavi-s3-imagebucket-test -File $PSItem.FullName -Verbose -Key $key + Write-S3Object -BucketName $bucketName -File $PSItem.FullName -Verbose -Key $key } - \ No newline at end of file + +# Delete a bucket +Get-S3Bucket $bucketName | + Remove-S3Bucket -Verbose -Force \ No newline at end of file diff --git a/04 - CloudWatch.ps1 b/04 - CloudWatch.ps1 index bc96ccd..c76bb9e 100644 --- a/04 - CloudWatch.ps1 +++ b/04 - CloudWatch.ps1 @@ -4,12 +4,13 @@ Get-CWLLogGroup -LogGroupNamePrefix '/aws/lambda/ResizeS3Photos' | # Find the log streams Get-CWLLogGroup -LogGroupNamePrefix '/aws/lambda/ResizeS3Photos' | - Get-CWLLogStream + Get-CWLLogStream | + Sort-Object -Descending CreationTime # Looking at the events -Get-CWLLogEvent -LogGroupName '/aws/lambda/ResizeS3Photos' -LogStreamName '2021/08/02/[$LATEST]effe97aece154f6a866db8d2b0621e92' +Get-CWLLogEvent -LogGroupName '/aws/lambda/ResizeS3Photos' -LogStreamName '2021/08/03/[$LATEST]348c1e96f50748299ffc361ca8ffbeb4' -Get-CWLLogEvent -LogGroupName '/aws/lambda/ResizeS3Photos' -LogStreamName '2021/08/02/[$LATEST]effe97aece154f6a866db8d2b0621e92' | +Get-CWLLogEvent -LogGroupName '/aws/lambda/ResizeS3Photos' -LogStreamName '2021/08/03/[$LATEST]348c1e96f50748299ffc361ca8ffbeb4' | Select-Object -ExpandProperty Events # A better log function diff --git a/05 - Lambda.ps1 b/05 - Lambda.ps1 index da123e4..9711dcf 100644 --- a/05 - Lambda.ps1 +++ b/05 - Lambda.ps1 @@ -9,14 +9,14 @@ Get-AWSPowerShellLambdaTemplate # Creating A Lambda New-AWSPowerShellLambda -Template basic -Directory 'C:\github\PowerShellAndAWS\BasicLambda' -Publish-AWSPowerShellLambda -ScriptPath 'C:\github\PowerShellAndAWS\BasicLambda\basic.ps1' -Name 'BasicLambda' -Verbose +# Publish Our Lambda +Publish-AWSPowerShellLambda -ScriptPath 'C:\github\PowerShellAndAWS\BasicLambda\basic.ps1' -Name 'BasicLambda' -Verbose +# Testing out the lambda Invoke-LMFunction -FunctionName 'BasicLambda' -LogType Tail -OutVariable BasicLmabda [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($BasicLmabda.LogResult)) - - # More Advanced New-AWSPowerShellLambda -Template S3Event -ProjectName ResizeS3Photos -WithProject -Verbose diff --git a/06 - CICD.ps1 b/06 - CICD.ps1 new file mode 100644 index 0000000..c69c753 --- /dev/null +++ b/06 - CICD.ps1 @@ -0,0 +1,31 @@ +# CDK Intro + +# Cloning a new Repo + +git clone https://git-codecommit.us-west-2.amazonaws.com/v1/repos/PowerShell-Repository + +# Generating the lambda package + +Set-Location -Path C:\github\PowerShell-Repository + +New-AWSPowerShellLambda -Template basic -Verbose +Copy-Item -Path C:\github\PowerShellAndAws\LambdaTemplateExample\buildspec.yaml -Destination . -Verbose +Code C:\github\PowerShell-Repository + +# Check In Our Code, Review the pipeline + + +# Invoke Our Function +Invoke-LMFunction -FunctionName 'PowerShellCICD-Sample' -LogType Tail +Get-CWLLastLogEvent -LogGroupNamePrefix '/aws/lambda/PowerShellCICD-Sample' + + +## Make a change and watch the build + +While($true) +{ + Start-Sleep -Seconds 5 + Invoke-LMFunction -FunctionName 'PowerShellCICD-Sample' -LogType Tail -Verbose + Get-CWLLastLogEvent -LogGroupNamePrefix '/aws/lambda/PowerShellCICD-Sample' +} + diff --git a/BasicLambda/basic.ps1 b/BasicLambda/basic.ps1 index 2e8e774..903cbaf 100644 --- a/BasicLambda/basic.ps1 +++ b/BasicLambda/basic.ps1 @@ -12,4 +12,5 @@ #Requires -Modules @{ModuleName='AWS.Tools.Common';ModuleVersion='4.1.14.0'} # Uncomment to send the input event to CloudWatch Logs -# Write-Host (ConvertTo-Json -InputObject $LambdaInput -Compress -Depth 5) +Write-Host (ConvertTo-Json -InputObject $LambdaInput -Compress -Depth 5) +Write-Host "Hello SoCalPosh" \ No newline at end of file diff --git a/HelperFunctions/Get-CWLLastLogEvent.ps1 b/HelperFunctions/Get-CWLLastLogEvent.ps1 index 0633224..4741128 100644 --- a/HelperFunctions/Get-CWLLastLogEvent.ps1 +++ b/HelperFunctions/Get-CWLLastLogEvent.ps1 @@ -5,11 +5,12 @@ Function Get-CWLLastLogEvent $LogGroupNamePrefix ) - $logStreams = Get-CWLLogGroup -LogGroupNamePrefix $LogGroupNamePrefix | + $logStream = Get-CWLLogGroup -LogGroupNamePrefix $LogGroupNamePrefix | Get-CWLLogStream | Sort-Object -desc -Property CreationTime | Select-Object -First 1 - Get-CWLLogEvent -LogGroupName $LogGroupNamePrefix -LogStreamName $logStreams.LogStreamName | - Select-Object -ExpandProperty Events -} \ No newline at end of file + $events = Get-CWLLogEvent -LogGroupName $LogGroupNamePrefix -LogStreamName $logStream.LogStreamName + $events.Events +} + diff --git a/LambdaTemplateExample/basic/basic.ps1 b/LambdaTemplateExample/basic/basic.ps1 new file mode 100644 index 0000000..45f2573 --- /dev/null +++ b/LambdaTemplateExample/basic/basic.ps1 @@ -0,0 +1,16 @@ +# PowerShell script file to be executed as a AWS Lambda function. +# +# When executing in Lambda the following variables will be predefined. +# $LambdaInput - A PSObject that contains the Lambda function input data. +# $LambdaContext - An Amazon.Lambda.Core.ILambdaContext object that contains information about the currently running Lambda environment. +# +# The last item in the PowerShell pipeline will be returned as the result of the Lambda function. +# +# To include PowerShell modules with your Lambda function, like the AWS.Tools.S3 module, add a "#Requires" statement +# indicating the module and version. If using an AWS.Tools.* module the AWS.Tools.Common module is also required. + +#Requires -Modules @{ModuleName='AWS.Tools.Common';ModuleVersion='4.1.14.0'} + +# Uncomment to send the input event to CloudWatch Logs +Write-Host (ConvertTo-Json -InputObject $LambdaInput -Compress -Depth 5) +Write-Host "Hello World" diff --git a/LambdaTemplateExample/basic/readme.txt b/LambdaTemplateExample/basic/readme.txt new file mode 100644 index 0000000..2e8e774 --- /dev/null +++ b/LambdaTemplateExample/basic/readme.txt @@ -0,0 +1,15 @@ +# PowerShell script file to be executed as a AWS Lambda function. +# +# When executing in Lambda the following variables will be predefined. +# $LambdaInput - A PSObject that contains the Lambda function input data. +# $LambdaContext - An Amazon.Lambda.Core.ILambdaContext object that contains information about the currently running Lambda environment. +# +# The last item in the PowerShell pipeline will be returned as the result of the Lambda function. +# +# To include PowerShell modules with your Lambda function, like the AWS.Tools.S3 module, add a "#Requires" statement +# indicating the module and version. If using an AWS.Tools.* module the AWS.Tools.Common module is also required. + +#Requires -Modules @{ModuleName='AWS.Tools.Common';ModuleVersion='4.1.14.0'} + +# Uncomment to send the input event to CloudWatch Logs +# Write-Host (ConvertTo-Json -InputObject $LambdaInput -Compress -Depth 5) diff --git a/LambdaTemplateExample/buildspec.yaml b/LambdaTemplateExample/buildspec.yaml new file mode 100644 index 0000000..9d17594 --- /dev/null +++ b/LambdaTemplateExample/buildspec.yaml @@ -0,0 +1,9 @@ +version: 0.2 + +phases: + install: + commands: + - pwsh -command Install-Module -Name AWSLambdaPSCore -Verbose -Force + build: + commands: + - pwsh -command Publish-AWSPowerShellLambda -ScriptPath ./basic/basic.ps1 -Name 'PowerShellCICD-Sample' -IAMRoleArn arn:aws:iam::124535484568:role/PowerShell-Lambda-ExecutionRole -Verbose