1

I try to use AWS Code Deploy with autoscaling groups. In the past i was deploying with octopus and i want to migrate to aws.

So, I have an Octopus Project with multiple steps. The first step deploys a cloudformation template which builds the infrastructure : vpc, subnets, Deployment groups, Autoscaling group, ELB, S3 Bucket, etc...

The other steps should substitute the variables of a an octopus package and retrieve the S3 bucket name from the first step to upload it to S3.

How i can achieve this with octopus?

Thanks.

Issa Dawaji
  • 183
  • 1
  • 12

1 Answers1

2

Here is how I accomplished that:

  1. Add a step to your process to deploy a package. I used a "Jumpbox" to deploy to, it just has to go somewhere.
  2. In the step to deploy the package click on configure features

  3. Select Custom Deployment Scripts

  4. Add this script as a deployment script. It will upload to the AWS S3 bucket after the variables have been replaced.

    $sourcePath = $OctopusParameters["Octopus.Action.Package.InstallationDirectoryPath"]
    $destinationPath = $OctopusParameters["Your parameter to the full path where you want the Zip file stored"]
    $destinationPathRootDirectory = $OctopusParameters["Your parameter to the folder"]
    
    Write-Host "Cleaning up previous zip files"
    Remove-Item "$destinationPathRootDirectory\*.zip"
    
    Write-Host "Compressing $sourcePath to $destinationPath"
    Compress-Archive -Path "$sourcePath\*"  -DestinationPath $destinationPath -Force
    
    $params = @{}
    
    #Initialises the S3 Credentials based on the Access Key and Secret Key provided, so that we can invoke the APIs further down
    Set-AWSCredentials -AccessKey $AwsAccessKey -SecretKey $AwsAccessSecret -StoreAs S3Creds
    
    #Initialises the Default AWS Region based on the region provided
    Set-DefaultAWSRegion -Region $AwsRegion
    
    #Gets all relevant files and uploads them
    function Upload($item) 
    {
        #Gets all files and child folders within the given directory
        foreach ($i in Get-ChildItem $item) {
                #Inserts file to AWS
                Write-S3Object -ProfileName S3Creds -BucketName $S3BucketName -Key $($i.Name) -File $i.FullName @params
        }
    }
    
    Upload($destinationPath)
    
  5. Add a step to run an AWS command line command which will perform the deployment.

Here is my sample script from that step:

    # All these parameters are parameters you set in the variables screen in Octopus Deploy

    Write-Host "ApplicationName $AwsApplicationName"
    Write-Host "DeploymentConfigName $AwsCodeDeployConfigName"
    Write-Host "CodeDeployName $AwsCodeDeployGroupName"
    Write-Host "BucketLocation $S3BucketName"
    Write-Host "KeyName $ZipFileName"

    # This is the AWS name of the location where you want to put the bucket, such as us-west-1
    Write-Host "BucketLocation $bucketLocation"

    $fullyQualifiedBucket = "$S3BucketName-$bucketLocation"
    Write-Host "Fully Qualified Bucket $fullyQualifiedBucket"

    $deployment = aws deploy create-deployment --application-name $AwsApplicationName --deployment-config-name $AwsCodeDeployConfigName --deployment-group-name $AwsCodeDeployGroupName --s3-location bucket=$S3BucketName,bundleType=zip,key=$TradingWebSiteZipFileName
    Write-Host "Deployment info $deployment"

It does create a unique package per environment in the S3 bucket. So you will have to account for that. It is not the world's most perfect solution, if you iterate through it and find a better one please let me know!

Bob Walker
  • 91
  • 3
  • I didn't understand your script, for example what is the $TradingWebSiteZipFullPath variable, what's the destination of this path? – Issa Dawaji May 23 '18 at 14:54
  • Sorry about that, the dangers of copy paste from a working example. That was a parameter that was sent in. You set it in the variables screen in Octopus deploy. For that particular variable, you need to specify a location on the server which both steps have access to. Mine was something like C:\CodeDeploy\[Project Name]\[Environment Name]\[FileName].zip I've updated my answer to hopefully make that more clear – Bob Walker May 24 '18 at 15:38