From 112d405d340a06a7fe57fa61cba7ba4c917ac907 Mon Sep 17 00:00:00 2001 From: Bryan Valencia Date: Sun, 10 Dec 2023 14:49:59 -0500 Subject: [PATCH] Adding buildspec file --- Dockerfile | 11 ++++ buildspec.yml | 27 ++++++++ cloud-formation-template.yaml | 120 ++++++++++++++++++++++++++++++++++ 3 files changed, 158 insertions(+) create mode 100644 Dockerfile create mode 100644 buildspec.yml create mode 100644 cloud-formation-template.yaml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 000000000..9b6c8218e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM openjdk:17-jdk + +WORKDIR /app + +COPY build/libs/spring-petclinic-3.2.0.jar /app/app.jar + +EXPOSE 8080 + +ENTRYPOINT ["java"] + +CMD ["-jar", "app.jar"] diff --git a/buildspec.yml b/buildspec.yml new file mode 100644 index 000000000..f1451d242 --- /dev/null +++ b/buildspec.yml @@ -0,0 +1,27 @@ +version: 0.2 + +phases: + install: + runtime-versions: + java: corretto11 + pre_build: + commands: + - echo Pre-build phase... + - echo Compile and running tests using Gradle Wrapper... + - chmod +x gradlew + - ./gradlew build + build: + commands: + - echo Build phase... + - echo Build Docker image and tag it... + - docker build -t bryanvalencia94/petclinic:$CODEBUILD_BUILD_NUMBER . + post_build: + commands: + - echo Post-build phase... + - echo Push Docker image to repository... + - docker push bryanvalencia94/petclinic:$CODEBUILD_BUILD_NUMBER + +#artifacts: +# files: +# - '**/*' +# name: DevOpsAppArtifacts diff --git a/cloud-formation-template.yaml b/cloud-formation-template.yaml new file mode 100644 index 000000000..d58dbf8f6 --- /dev/null +++ b/cloud-formation-template.yaml @@ -0,0 +1,120 @@ +Resources: + SecurityGroupPetClinic: + Type: "AWS::EC2::SecurityGroup" + Properties: + GroupName: "SecurityGroupPetClinic" + GroupDescription: "Security group to allow SSH and HTTP over 8080" + VpcId: "vpc-fe0d6d84" + SecurityGroupEgress: + - IpProtocol: "-1" + CidrIp: 0.0.0.0/0 + SecurityGroupIngress: + - IpProtocol: tcp + FromPort: 22 + ToPort: 22 + CidrIp: 0.0.0.0/0 + - IpProtocol: tcp + FromPort: 8080 + ToPort: 8080 + CidrIp: 0.0.0.0/0 + - IpProtocol: tcp + FromPort: 80 + ToPort: 80 + CidrIp: 0.0.0.0/0 + + LaunchConfigurationPetClinic2: + Type: "AWS::AutoScaling::LaunchConfiguration" + Properties: + ImageId: "ami-04c97e62cb19d53f1" + InstanceType: "t4g.small" + KeyName: "devops-keypair" + LaunchConfigurationName: "LaunchConfigurationPetClinic2" + SecurityGroups: + - !GetAtt SecurityGroupPetClinic.GroupId + UserData: + Fn::Base64: + !Sub | + #!/bin/bash + sudo yum update -y + sudo yum install docker -y + sudo service docker start + sudo docker pull bryanvalencia94/petclinic:1.0.0-mac-arm + sudo docker run -p 8080:8080 bryanvalencia94/petclinic:1.0.0-mac-arm + + AutoScalingGroupPetClinic: + Type: "AWS::AutoScaling::AutoScalingGroup" + Properties: + MaxSize: 2 + MinSize: 1 + LaunchConfigurationName: + !Ref LaunchConfigurationPetClinic2 + VPCZoneIdentifier: + - subnet-3da0f161 + - subnet-3304e07e + TargetGroupARNs: + - Ref: TargetGroupPetClinic + + AutoScalingPolicyPetClinic: + Type: AWS::AutoScaling::ScalingPolicy + Properties: + AdjustmentType: "ChangeInCapacity" + AutoScalingGroupName: + !Ref AutoScalingGroupPetClinic + Cooldown: "300" + ScalingAdjustment: "1" + + LoadBalancerPetClinic: + Type: "AWS::ElasticLoadBalancingV2::LoadBalancer" + Properties: + Name: LoadBalancerPetClinic + Subnets: + - subnet-3da0f161 + - subnet-3304e07e + SecurityGroups: + - !Ref SecurityGroupPetClinic + Type: application + + TargetGroupPetClinic: + Type: "AWS::ElasticLoadBalancingV2::TargetGroup" + Properties: + Name: TargetGroupPetClinic + Port: 8080 + Protocol: HTTP + VpcId: "vpc-fe0d6d84" + TargetType: instance + + ListenerPetClinic: + Type: "AWS::ElasticLoadBalancingV2::Listener" + Properties: + DefaultActions: + - Type: forward + TargetGroupArn: + Ref: TargetGroupPetClinic + LoadBalancerArn: + Ref: LoadBalancerPetClinic + Port: 80 + Protocol: HTTP + + CloudFrontDistributionPetClinic: + Type: "AWS::CloudFront::Distribution" + Properties: + DistributionConfig: + DefaultCacheBehavior: + TargetOriginId: LoadBalancerOrigin + ViewerProtocolPolicy: allow-all + CachePolicyId: 658327ea-f89d-4fab-a63d-7e88639e58f6 # This is the AWS Managed CacheOptimized CachePolicyId + Enabled: true + Origins: + - CustomOriginConfig: + HTTPPort: 80 + OriginProtocolPolicy: http-only + DomainName: !GetAtt LoadBalancerPetClinic.DNSName + Id: LoadBalancerOrigin + +Outputs: + LoadBalancerDNSName: + Description: The DNS name of the load balancer. + Value: + Fn::GetAtt: + - LoadBalancerPetClinic + - DNSName