How to deal with large WAR files in AWS?

What I've done?

Developed a web application using JSP that allows a user to register, upload, and upload files to AWS S3. I am using this app for aws using Elastic Beanstalk by downloading the application war file. For registration and registration modules, I used RDS and it works fine.

Problem

When I want to upload files to S3, I need to use the AWS SDK jar and its JAR-supporting files in the WEB application. When I finished the development part and exported the war file, it was about 75 MB. So the problem is that if I change something in the application, I need to load this huge war into aws. The war file size is due to the AWS SDK JAR files.

What are the options for solving this situation?

+4
source share
2 answers

Use build tools like Maven. This will ensure that dependency files are uploaded to the local storage of the maven server. Thus, the load size of your project is reduced. Here is the official directory

+1
source

Steps for deploying partial (thus smaller) war files using Elastic Beanstalk:

  1. create a separate package (for example dist-lib.zip) with external libraries used
  2. download this package on S3
  3. create configElastic Beanstalk deployment files to load and extract external libraries on EC2 nodes
  4. war
  5. 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; // replace w/ your version number
        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

:

  1. ;
  2. - , - , .

3. EB

S3 Amazon S3.

, war , webapps Tomcat.

deploy/eb/app-res.config:

Resources:
  # Use instance profile to authenticate to S3 bucket that contains the private key
  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:
  # distribution libs
  /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: |
      #!/usr/bin/env bash
      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).

0

Source: https://habr.com/ru/post/1627912/


All Articles