From 95bd0d8920ae0efdd26b840c4c9cda35cbfbf7d1 Mon Sep 17 00:00:00 2001 From: "Richard T. Miles" Date: Fri, 22 Aug 2025 13:06:35 -0600 Subject: [PATCH] chore: remove static ALB template --- .github/assets/php/createAlbYaml.php | 165 +++++++++++++-------------- CloudFormation/web.yaml | 20 +++- README.md | 58 ++++++++++ 3 files changed, 151 insertions(+), 92 deletions(-) diff --git a/.github/assets/php/createAlbYaml.php b/.github/assets/php/createAlbYaml.php index 1536e4e..55fd27c 100644 --- a/.github/assets/php/createAlbYaml.php +++ b/.github/assets/php/createAlbYaml.php @@ -1,129 +1,122 @@ $certificate) { - $httpsListener .= << + Description: Default hostnames handled by the listener +EOT; - EOF; +if (!empty($defaultHosts)) { + $defaultHostsParam .= "\n Default: $defaultHosts"; } -print << - Description: List of Private subnets to use for the application + Description: Subnets for the load balancer + LoadBalancerSecurityGroups: + Type: List + Description: Security groups for the load balancer +$defaultCertificateParam +$defaultHostsParam Resources: PublicAlb: Type: AWS::ElasticLoadBalancingV2::LoadBalancer Properties: - IpAddressType: ipv4 - Name: publicAlb Scheme: internet-facing - SecurityGroups: - - !ImportValue Ec2SecurityGroup Subnets: !Ref PublicSubnets + SecurityGroups: !Ref LoadBalancerSecurityGroups + Type: application Tags: - Key: Name - Value: ec2-alb - Type: application + Value: publicAlb PublicAlbHttpListener: Type: AWS::ElasticLoadBalancingV2::Listener Properties: - DefaultActions: -$DefaultHttpAction LoadBalancerArn: !Ref PublicAlb Port: 80 Protocol: HTTP + DefaultActions: + - Type: redirect + RedirectConfig: + Port: '443' + Protocol: HTTPS + StatusCode: HTTP_301 -$httpsListener + PublicAlbHttpsListener: + Type: AWS::ElasticLoadBalancingV2::Listener + Properties: + LoadBalancerArn: !Ref PublicAlb + Port: 443 + Protocol: HTTPS + Certificates: + - CertificateArn: !Ref DefaultCertificateArn + DefaultActions: + - Type: fixed-response + FixedResponseConfig: + StatusCode: '404' + ContentType: text/plain + MessageBody: Not Found + + DefaultHttpsListenerRule: + Type: AWS::ElasticLoadBalancingV2::ListenerRule + Properties: + Actions: + - Type: fixed-response + FixedResponseConfig: + StatusCode: '404' + ContentType: text/plain + MessageBody: Not Found + Conditions: + - Field: host-header + HostHeaderConfig: + Values: !Ref DefaultLoadBalancerHosts + ListenerArn: !Ref PublicAlbHttpsListener + Priority: 1 Outputs: - PublicAlb: + PublicAlbArn: Value: !Ref PublicAlb Export: Name: PublicAlbArn - PublicAlbCanonicalHostedZoneId: - Value: !GetAtt PublicAlb.CanonicalHostedZoneID PublicAlbDnsName: Value: !GetAtt PublicAlb.DNSName - PublicAlbFullName: - Value: !GetAtt PublicAlb.LoadBalancerFullName - PublicAlbHostname: - Value: !Sub https://\${PublicAlb.DNSName} + Export: + Name: PublicAlbDnsName + PublicAlbCanonicalHostedZoneId: + Value: !GetAtt PublicAlb.CanonicalHostedZoneID + Export: + Name: PublicAlbCanonicalHostedZoneId PublicAlbHttpListenerArn: Value: !Ref PublicAlbHttpListener Export: Name: PublicAlbHttpListenerArn -$PublicAlbHttpsListenerReturn - -EOF; - + PublicAlbHttpsListenerArn: + Value: !Ref PublicAlbHttpsListener + Export: + Name: PublicAlbHttpsListenerArn +YAML; diff --git a/CloudFormation/web.yaml b/CloudFormation/web.yaml index 2eb8f84..1036964 100755 --- a/CloudFormation/web.yaml +++ b/CloudFormation/web.yaml @@ -112,9 +112,9 @@ Parameters: Description: Add a UDP listener to the NLB Default: "false" - CertificateArns: - Type: CommaDelimitedList # List - Description: List of ACM certificates to be used by the load balancer listener + CertificateArn: + Type: String + Description: ACM certificate to attach to the load balancer listener Default: "" UseGitHubRunNumberForASG: @@ -128,8 +128,8 @@ Parameters: Conditions: linkAlb: !Equals [ !Ref AddAlbListener, 'true' ] linkNlb: !Equals [ !Ref AddNlbListener, 'true' ] - HasCertificates: !Not [ !Equals [ !Join [ "", !Ref CertificateArns ], "" ] ] - linkAlbWithCerts: !And [ !Condition linkAlb, !Condition HasCertificates ] + HasCertificate: !Not [ !Equals [ !Ref CertificateArn, "" ] ] + linkAlbWithCert: !And [ !Condition linkAlb, !Condition HasCertificate ] HasLoadBalancerHosts: !Not [ !Equals [ !Join [ "", !Ref LoadBalancerHosts], "" ] ] IncludeGitHubRunNumberForASG: !Equals [!Ref UseGitHubRunNumberForASG, 'true'] @@ -176,8 +176,16 @@ Resources: ListenerArn: !ImportValue PublicAlbHttpListenerArn Priority: !Ref LoadBalancerRulePriority + AlbHttpsListenerCertificate: + Condition: linkAlbWithCert + Type: AWS::ElasticLoadBalancingV2::ListenerCertificate + Properties: + Certificates: + - CertificateArn: !Ref CertificateArn + ListenerArn: !ImportValue PublicAlbHttpsListenerArn + AlbHttpsListenerRule: - Condition: linkAlbWithCerts + Condition: linkAlb Type: AWS::ElasticLoadBalancingV2::ListenerRule Properties: Actions: diff --git a/README.md b/README.md index 3ead627..3d99333 100644 --- a/README.md +++ b/README.md @@ -96,6 +96,64 @@ https://docs.aws.amazon.com/cloudformation/ This is a full example of a GitHub Actions workflow that utalizes this repository. +The shared Application Load Balancer stack is generated on the fly using the +`createAlbYaml.php` helper: + +```bash +php .github/assets/php/createAlbYaml.php "$DEFAULT_CERT" "$DEFAULT_HOSTS" > CloudFormation/alb.yaml +``` + +### Branch-based deployment snippet + +The following job derives CloudFormation parameters from **CURRENT_BRANCH**, +**FLAVOR**, and **DOMAINS** so each workflow creates its own listener rule and +certificate: + +```yaml +jobs: + deploy: + runs-on: ubuntu-latest + env: + DEFAULT_BRANCH: www + CURRENT_BRANCH: ${{ github.ref_name }} + FLAVOR: ${{ inputs.flavor }} + steps: + - name: Compute parameters + id: params + run: | + if [ "$FLAVOR" = "openreplay" ]; then + DOMAIN="or.$CURRENT_BRANCH.assessorly.com" + DB_ENGINE="aurora-postgresql" + if [ "$CURRENT_BRANCH" = "$DEFAULT_BRANCH" ]; then + VOLUME=100 + else + VOLUME=50 + fi + elif [ "$CURRENT_BRANCH" = "$DEFAULT_BRANCH" ]; then + DOMAIN="www.assessorly.com" + DB_ENGINE="aurora-mysql" + VOLUME=50 + else + DOMAIN="$CURRENT_BRANCH.assessorly.com" + DB_ENGINE="aurora-mysql" + VOLUME=50 + fi + echo "domain=$DOMAIN" >> $GITHUB_OUTPUT + echo "engine=$DB_ENGINE" >> $GITHUB_OUTPUT + echo "volume=$VOLUME" >> $GITHUB_OUTPUT + - name: Deploy stack + run: | + aws cloudformation deploy \ + --template-file CloudFormation/web.yaml \ + --stack-name web-$CURRENT_BRANCH \ + --parameter-overrides \ + LoadBalancerHosts=${{ steps.params.outputs.domain }} \ + CertificateArn=$ACM_CERT \ + DatabaseEngine=${{ steps.params.outputs.engine }} \ + AllocatedStorage=${{ steps.params.outputs.volume }} \ + LoadBalancerRulePriority=100 +``` + ```yaml name: Aws Deployment Workflow