Steps for deploying partial (thus smaller) war files using Elastic Beanstalk:
- create a separate package (for example
dist-lib.zip
) with external libraries used - download this package on S3
- create
config
Elastic Beanstalk deployment files to load and extract external libraries on EC2 nodes war
war
1. dist-lib.zip
build.gradle
- ( zip):
apply plugin: 'java-library-distribution'
distTar.enabled = false
distZip.enabled = hasProperty('dist')
def subPrjs = rootProject.subprojects.collect { it.name } - project.name
distributions.main {
contents.exclude subPrjs.collect { "$it*.jar" }
contents.exclude "tomcat-embed-*"
contents.exclude "tomcat-annotations-api-*"
contents.exclude "spring-boot-starter-tomcat-*"
}
distZip {
baseName += '-lib'
version = null
doLast {
def f0 = archivePath;
version = props.ver;
if (archivePath.exists()) {
archivePath.delete()
}
f0.renameTo(archivePath.path)
}
}
zip w/: gradle distZip -Pdist=true
.
2. dist-lib.zip
S3
aws-cli: aws s3 cp YOUR_MODULE/build/distributions/YOUR_MODULE-lib-YOUR_VERSION.zip s3://YOUR_BUCKET/dist/dist-lib.zip
:
- ;
- - , - , .
3. EB
S3 Amazon S3.
, war , webapps
Tomcat.
deploy/eb/app-res.config
:
Resources:
AWSEBAutoScalingGroup:
Metadata:
AWS::CloudFormation::Authentication:
S3Auth:
type: "s3"
buckets: ["elasticbeanstalk-us-east-1-169305339676"]
roleName:
"Fn::GetOptionSetting":
Namespace: "aws:autoscaling:launchconfiguration"
OptionName: "IamInstanceProfile"
DefaultValue: "aws-elasticbeanstalk-ec2-role-dev"
files:
/tmp/dist-lib.zip:
mode: "000644"
owner: tomcat
group: tomcat
authentication: "S3Auth"
source: https://s3.amazonaws.com/YOUR_BUCKET/dist/dist-lib.zip
deploy/eb/dist-lib.config
:
files:
/opt/elasticbeanstalk/hooks/appdeploy/pre/10_lib_extr.sh:
mode: "000755"
owner: root
group: root
content: |
rm -rf /tmp/dist-lib
unzip /tmp/dist-lib.zip -d /tmp/
mv /tmp/dist-lib/lib /tmp/deployment/application/ROOT/WEB-INF/lib
4. war
build.gradle
- ( () WEB-INF/classes
):
apply plugin: 'war'
jar.enabled = false
def env = project.hasProperty('env') ? project.getProperty('env') : 'lcl' // profile: set w/ "-Penv=..."
def ebDirs = ["$rootDir/deploy/eb-$env", "$rootDir/deploy/eb", "$rootDir/deploy/eb/_nginx"] // env. specific config first
// ...
war {
duplicatesStrategy = 'fail'
//rootSpec.exclude subPrjs.collect { "**/$it*.jar" } // exclude subproject jars only
rootSpec.exclude "**/*.jar" // exclude dependencies => they must be downloaded and extracted during deployment
from(subPrjs.collect { project(":$it").sourceSets.main.output }) {
duplicatesStrategy = 'exclude' // in case of one output dir for multiple sourceSets
into "WEB-INF/classes"
}
from(ebDirs) {
duplicatesStrategy = 'exclude' // in case of env. spec. config
exclude "**/_*"
into ".ebextensions"
}
}
5.
w/gradle v4.10.2
aws-cli v1.16.44
( w/eb create
eb deploy
).