Upgrade to Pro — share decks privately, control downloads, hide ads and more …

AWS IoT Greengrass V2で始める デバイスアプリケーションの継続的開発

wanda
July 19, 2023

AWS IoT Greengrass V2で始める デバイスアプリケーションの継続的開発

DevelopersIO 2023 大阪にて登壇時に使用した資料です。

wanda

July 19, 2023
Tweet

More Decks by wanda

Other Decks in Technology

Transcript

  1. AWS IoT Greengrass V
    2

    2
    02
    3
    /
    07
    /
    19
    AWS

    View Slide



  2. AWS IoT Greengrass


    Greengrass Development Kit GDK


    GDK CICD


    CICD
    2

    View Slide



  3. AWS IoT Greengrass


    AWS CodeCommit, AWS Pipeline, AWS CodeBuild
    3

    View Slide

  4. Greengrass


    4

    View Slide



  5. myapp.py


    Recipe


    zip maven jar


    S
    3

    S
    3
    Greengrass


    5

    View Slide



  6. myapp.py


    Recipe


    zip maven jar


    S
    3

    S
    3
    Greengrass


    6

    View Slide

  7. 7
    S
    3
    ʲॻࣜʳ


    s3://BUCKET_NAME/COMPONENT_NAME/COMPONENT_VERSION/myapp.zip
    s3://BUCKET_NAME/COMPONENT_NAME/1.0.0/myapp.zip


    s3://BUCKET_NAME/COMPONENT_NAME/1.0.1/myapp.zip


    s3://BUCKET_NAME/COMPONENT_NAME/1.0.2/myapp.zip

    View Slide

  8. Greengrass Development Kit
    8
    Greengrass


    Greengrass


    Recipe( ) GDK (gdk-con
    fi
    g.json)


    URI


    View Slide

  9. GDK 2
    GDK
    9
    $ gdk component build


    $ gdk component publish


    View Slide





  10. zip jar


    S
    3

    S
    3



    1
    0
    ʩ
    GDKͰࣗಈԽʂ

    View Slide

  11. 1
    1
    GDK Λ CI πʔϧͰ࢖͑͹


    ߋʹ։ൃ͕ޮ཰తʹͳΔ

    View Slide

  12. GDK CICD
    1
    2

    View Slide

  13. CICD
    1
    3 


    View Slide

  14. AWS CLI GDK CLI


    GDK


    GDK


    1
    4

    View Slide

  15. Buildspec
    1
    5
    phases:


    install:


    commands:


    - curl "https://awscli.amazonaws.com/awscli-exe-linux-$ARCH.zip" -o "awscliv2.zip"


    - ./aws/install -i /usr/local/aws-cli -b /usr/local/bin --update


    - CURRDIR=$(basename "$PWD")


    - cd ../ && mv $CURRDIR devio2023App && cd devio2023App


    - python3 -m venv buildenv


    - . ./buildenv/bin/activate


    # Install GDK CLI


    - python3 -m pip install -U git+https://github.com/aws-greengrass/[email protected]


    build:


    commands:


    - gdk component build


    - gdk component publish

    View Slide

  16. CodeBuild CodeCommit myrepo


    GDK




    S
    3

    URI


    Buildspec
    1
    6


    Ϗϧυ࡞ۀ͢ΔσΟϨΫτϦ໊ΛΞʔςΟϑΝΫτ໊ʹมߋ


    - CURRDIR=$(basename "$PWD")


    - cd ../ && mv $CURRDIR devio2023App && cd devio2023App


    Artifacts:


    - URI: "s3://BUCKET_NAME/COMPONENT_NAME/COMPONENT_VERSION/devio2023App.zip"


    Unarchive: ZIP

    View Slide

  17. CodeBuild CodeCommit myrepo


    GDK




    S
    3

    URI


    Buildspec
    1
    7


    Ϗϧυ࡞ۀ͢ΔσΟϨΫτϦ໊ΛΞʔςΟϑΝΫτ໊ʹมߋ


    - CURRDIR=$(basename "$PWD")


    - cd ../ && mv $CURRDIR devio2023App && cd devio2023App


    Artifacts:


    - URI: "s3://BUCKET_NAME/COMPONENT_NAME/COMPONENT_VERSION/devio2023App.zip"


    Unarchive: ZIP
    NZSFQP㱠EFWJP"QQ
    ίϯϙʔωϯτϏϧυ/(

    View Slide






  18. CreateDeployment
    1
    8

    View Slide

  19. Buildspec
    1
    9
    phases:


    install:


    commands:


    - curl "https://awscli.amazonaws.com/awscli-exe-linux-$ARCH.zip" -o "awscliv2.zip"


    - unzip awscliv2.zip


    - ./aws/install -i /usr/local/aws-cli -b /usr/local/bin --update


    build:


    commands:


    - AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query 'Account' --output text)

    - AWS_REGION=ap-northeast-1


    - COMPONENT_NAME=$(cat gdk-con
    fi
    g.json | jq -r '.component | keys[]’)


    - chmod +x ./deploy-components.sh


    - sh -x ./deploy-components.sh ${AWS_ACCOUNT_ID}


    ɹɹ${AWS_REGION} ${COMPONENT_NAME} ${IOT_THING_GROUP} ${DEPLOYMENT_NAME}
    #ݟ΍͢͞ͷͨΊվߦ

    View Slide

  20. 2
    0
    # ର৅ίϯϙʔωϯτͷ࠷৽όʔδϣϯ਺Λऔಘ


    LATEST_COMP_VERSION=$(aws greengrassv2 list-components | \


    jq -r '.components[] | select(.componentName == "'"${COMPONENT_NAME}"'").latestVersion' | jq -r '.componentVersion')


    # ࠷৽ͷσϓϩΠϝϯτIDΛऔಘ


    DEPLOYMENT_ID=$(aws greengrassv2 list-deployments --target-arn ${THING_GROUP_ARN} | jq -r '.deployments[]' | jq -r .deploymentId)


    # ର৅σϓϩΠϝϯτIDʹ΋ͱ͍ͮͨσϓϩΠఆٛ৘ใΛऔಘɺ৽نʹσϓϩΠఆٛΛ࡞੒


    NEW_CONFIG_JSON=$(aws greengrassv2 get-deployment \


    --deployment-id ${DEPLOYMENT_ID} | jq .'components' | jq 'del(."$COMP_NAME")' | \


    jq '. += {"'"$COMPONENT_NAME"'": {"componentVersion": "'"$LATEST_COMP_VERSION"'","con
    fi
    gurationUpdate":{"reset":[""]}}}')


    FINAL_CONFIG_JSON='{"components":'$NEW_CONFIG_JSON'}'


    echo $(echo "$FINAL_CONFIG_JSON" | jq '.') > ${DEPLOYMENT_CONFIG_FILE}


    # σϓϩΠϝϯτͷ࡞੒ʢσϓϩΠ࣮ࢪʣ


    ## set con
    fi
    g
    fi
    le URI


    CONFIG_URI=“
    fi
    leb://${DEPLOYMENT_CONFIG_FILE}”


    ## Deploy


    aws greengrassv2 create-deployment --target-arn ${THING_GROUP_ARN} \


    --deployment-name ${DEPLOYMENT_NAME} \


    --cli-input-json ${CONFIG_URI}

    View Slide

  21. 2
    1
    # ࠷৽ͷσϓϩΠϝϯτIDΛऔಘ


    DEPLOYMENT_ID=$(aws greengrassv2 list-deployments --target-arn ${THING_GROUP_ARN} | \


    jq -r '.deployments[]' | jq -r .deploymentId)


    ɾෳ਺ίϯϙʔωϯτ͕σϓϩΠࡁΈͷ৔߹ɺͦΕΒͷσϓϩΠ৘ใ΋ؚΊͯ

    ɹσϓϩΠ͢Δඞཁ͕͋ΔͨΊɺ௚લͷσϓϩΠϝϯτ*%Λऔಘ
    ɾσϓϩΠ৘ใͷͳ͍ίϯϙʔωϯτ͸ʮ࡟আʯ͞ΕΔ

    View Slide

  22. 2
    2
    # ର৅σϓϩΠϝϯτIDʹ΋ͱ͍ͮͨσϓϩΠఆٛ৘ใΛऔಘɺ৽نʹσϓϩΠఆٛΛ࡞੒


    NEW_CONFIG_JSON=$(aws greengrassv2 get-deployment \


    --deployment-id ${DEPLOYMENT_ID} | jq .'components' | jq 'del(."$COMP_NAME")' | \


    jq '. += {"'"$COMPONENT_NAME"'": {"componentVersion":
    "'"$LATEST_COMP_VERSION"'","con
    fi
    gurationUpdate":{"reset":[""]}}}')


    FINAL_CONFIG_JSON='{"components":'$NEW_CONFIG_JSON'}'


    echo $(echo "$FINAL_CONFIG_JSON" | jq '.') > ${DEPLOYMENT_CONFIG_FILE}


    ɾଞͷσϓϩΠࡁΈίϯϙʔωϯτͷσϓϩΠ৘ใΛऔಘ
    ɾ͜ͷεςʔδͰσϓϩΠ͢Δίϯϙʔωϯτͷ࠷৽όʔδϣϯΛؚΊͨσϓϩΠ

    ɹఆٛΛ࡞੒

    View Slide

  23. 2
    3
    # σϓϩΠϝϯτͷ࡞੒ʢσϓϩΠ࣮ࢪʣ


    ## set con
    fi
    g
    fi
    le URI


    CONFIG_URI=“
    fi
    leb://${DEPLOYMENT_CONFIG_FILE}”


    ## Deploy


    aws greengrassv2 create-deployment --target-arn ${THING_GROUP_ARN} \


    --deployment-name ${DEPLOYMENT_NAME} \


    --cli-input-json ${CONFIG_URI}


    ɾ࡞੒ͨ͠σϓϩΠఆٛΛݩʹɺσόΠε΁ͷσϓϩΠʢδϣϒʣΛ࣮ߦ
    ɹɾσϓϩΠఆٛϑΝΠϧΛΦϓγϣϯͰ౉͢

    View Slide



  24. JSON


    ( push)


    2
    4

    View Slide

  25. Greengrass


    2
    5

    View Slide



  26. Greengrass


    PHP
    Greengrass
    2
    6

    View Slide



  27. ARM, AMD, RISC-V


    3 2
    bit,
    6
    4
    bit


    CodeBuild x
    86
    _
    6 4
    , aarch
    6 4
    x
    8 6 32
    bit
    Greengrass Docker
    2
    7

    View Slide






  28. Docker Buildx


    multiarch/qemu-user-static


    CPU
    2
    8
    ηοτΞοϓʹ͕͔͔࣌ؒΔ
    ख͕͔͔ؒΓ͗͢Δ
    QEMUͱbinfmt_miscΛ࢖༻ͯ͠ҟͳΔϚϧνΞʔΩςΫνϟͷ
    ଟ͘ͷΞʔΩςΫνϟΛαϙʔτ
    ίϯςφΛ࣮ߦ͢ΔͨΊͷDockerΠϝʔδ

    View Slide

  29. CPU


    2
    9

    View Slide

  30. AWS
    3
    0

    View Slide

  31. CodeBuild


    CPU



    ECR Docker Manifest List


    Manifest List


    AWS
    3
    1

    View Slide

  32. Greengrass Docker


    Greengrass


    Greengrass
    3
    2
    ComponentCon
    fi
    guration:


    DefaultCon
    fi
    guration:


    MY_TAG: latest


    ʙதུʙ


    Manifests:


    - Lifecycle:


    Run: "docker container run … xxx.dkr.ecr.xxx.amazonaws.com/myrepo:{con
    fi
    guration:/MY_TAG}"


    Artifacts:


    - Uri: “docker:xxx.dkr.ecr.xxx.amazonaws.com/myrepo:{con
    fi
    guration:/MY_TAG}”


    Ϩγϐม਺͸
    -JGFDZDMF಺ͷΈ

    View Slide





  33. docker pull, docker run


    Greengrass
    3
    3
    ComponentCon
    fi
    guration:


    DefaultCon
    fi
    guration:


    MY_TAG: latest


    ʙதུʙ


    Manifests:


    - Lifecycle:


    Run: sh -x {artifacts:path}/docker-component-run.sh ‘{con
    fi
    guration:/MY_TAG}’


    Artifacts:


    - Uri: s3://όέοτ໊/.../artifacts/ίϯϙʔωϯτ໊/x.x.x/docker-component-run.sh

    View Slide

  34. 3
    4

    View Slide

  35. Greengrass GDK





    CPU


    multiarch/qemu-user-static


    CodeBuild


    Greengrass
    3
    5

    View Slide

  36. View Slide