├── .Rbuildignore ├── .github ├── .gitignore ├── FUNDING.yml └── workflows │ ├── R-CMD-check.yaml │ ├── pkgdown.yaml │ └── test-coverage.yaml ├── .gitignore ├── CODE_OF_CONDUCT.md ├── DESCRIPTION ├── LICENSE.md ├── NAMESPACE ├── R ├── lambda-deploy.R ├── lambda-util.R ├── s3.R └── zzz.R ├── README.Rmd ├── README.md ├── _pkgdown.yml ├── codecov.yml ├── inst ├── lambda-S3-put-role.json ├── lambda-role.json ├── lambdr_dockerfile.txt ├── parity.R └── renv.lock ├── man ├── aws_connect.Rd ├── build_lambda.Rd ├── deploy_lambda.Rd ├── invoke_lambda.Rd ├── schedule_lambda.Rd └── test_lambda.Rd ├── r2lambda.Rproj ├── tests ├── testthat.R └── testthat │ └── test-lambda-util.R └── vignettes ├── .gitignore ├── lambda-s3.Rmd ├── schedule-lambda.Rmd └── tt-lambda.Rmd /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | ^README\.Rmd$ 4 | ^LICENSE\.md$ 5 | ^\.github$ 6 | ^codecov\.yml$ 7 | ^docs/ 8 | ^CODE_OF_CONDUCT\.md$ 9 | ^doc$ 10 | ^Meta$ 11 | ^_pkgdown\.yml$ 12 | ^docs$ 13 | ^pkgdown$ 14 | -------------------------------------------------------------------------------- /.github/.gitignore: -------------------------------------------------------------------------------- 1 | *.html 2 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | # These are supported funding model platforms 2 | 3 | github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] 4 | patreon: # Replace with a single Patreon username 5 | open_collective: # Replace with a single Open Collective username 6 | ko_fi: discindo 7 | tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel 8 | community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry 9 | liberapay: # Replace with a single Liberapay username 10 | issuehunt: # Replace with a single IssueHunt username 11 | lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry 12 | polar: # Replace with a single Polar username 13 | buy_me_a_coffee: # Replace with a single Buy Me a Coffee username 14 | thanks_dev: # Replace with a single thanks.dev username 15 | custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] 16 | -------------------------------------------------------------------------------- /.github/workflows/R-CMD-check.yaml: -------------------------------------------------------------------------------- 1 | # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples 2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help 3 | on: 4 | push: 5 | branches: [main, master] 6 | pull_request: 7 | branches: [main, master] 8 | 9 | name: R-CMD-check 10 | 11 | jobs: 12 | R-CMD-check: 13 | runs-on: ${{ matrix.config.os }} 14 | 15 | name: ${{ matrix.config.os }} (${{ matrix.config.r }}) 16 | 17 | strategy: 18 | fail-fast: false 19 | matrix: 20 | config: 21 | - {os: macos-latest, r: 'release'} 22 | - {os: windows-latest, r: 'release'} 23 | - {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'} 24 | - {os: ubuntu-latest, r: 'release'} 25 | - {os: ubuntu-latest, r: 'oldrel-1'} 26 | 27 | env: 28 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 29 | R_KEEP_PKG_SOURCE: yes 30 | 31 | steps: 32 | - uses: actions/checkout@v3 33 | 34 | - uses: r-lib/actions/setup-pandoc@v2 35 | 36 | - uses: r-lib/actions/setup-r@v2 37 | with: 38 | r-version: ${{ matrix.config.r }} 39 | http-user-agent: ${{ matrix.config.http-user-agent }} 40 | use-public-rspm: true 41 | 42 | - uses: r-lib/actions/setup-r-dependencies@v2 43 | with: 44 | extra-packages: any::rcmdcheck 45 | needs: check 46 | 47 | - uses: r-lib/actions/check-r-package@v2 48 | with: 49 | upload-snapshots: true 50 | -------------------------------------------------------------------------------- /.github/workflows/pkgdown.yaml: -------------------------------------------------------------------------------- 1 | # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples 2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help 3 | on: 4 | push: 5 | branches: [main, master] 6 | pull_request: 7 | branches: [main, master] 8 | release: 9 | types: [published] 10 | workflow_dispatch: 11 | 12 | name: pkgdown 13 | 14 | jobs: 15 | pkgdown: 16 | runs-on: ubuntu-latest 17 | # Only restrict concurrency for non-PR jobs 18 | concurrency: 19 | group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }} 20 | env: 21 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 22 | steps: 23 | - uses: actions/checkout@v3 24 | 25 | - uses: r-lib/actions/setup-pandoc@v2 26 | 27 | - uses: r-lib/actions/setup-r@v2 28 | with: 29 | use-public-rspm: true 30 | 31 | - uses: r-lib/actions/setup-r-dependencies@v2 32 | with: 33 | extra-packages: any::pkgdown, local::. 34 | needs: website 35 | 36 | - name: Build site 37 | run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE) 38 | shell: Rscript {0} 39 | 40 | - name: Deploy to GitHub pages 🚀 41 | if: github.event_name != 'pull_request' 42 | uses: JamesIves/github-pages-deploy-action@v4.4.1 43 | with: 44 | clean: false 45 | branch: gh-pages 46 | folder: docs 47 | -------------------------------------------------------------------------------- /.github/workflows/test-coverage.yaml: -------------------------------------------------------------------------------- 1 | # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples 2 | # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help 3 | on: 4 | push: 5 | branches: [main, master] 6 | pull_request: 7 | branches: [main, master] 8 | 9 | name: test-coverage 10 | 11 | jobs: 12 | test-coverage: 13 | runs-on: ubuntu-latest 14 | env: 15 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 16 | 17 | steps: 18 | - uses: actions/checkout@v3 19 | 20 | - uses: r-lib/actions/setup-r@v2 21 | with: 22 | use-public-rspm: true 23 | 24 | - uses: r-lib/actions/setup-r-dependencies@v2 25 | with: 26 | extra-packages: any::covr 27 | needs: coverage 28 | 29 | - name: Test coverage 30 | run: | 31 | covr::codecov( 32 | quiet = FALSE, 33 | clean = FALSE, 34 | install_path = file.path(Sys.getenv("RUNNER_TEMP"), "package") 35 | ) 36 | shell: Rscript {0} 37 | 38 | - name: Show testthat output 39 | if: always() 40 | run: | 41 | ## -------------------------------------------------------------------- 42 | find ${{ runner.temp }}/package -name 'testthat.Rout*' -exec cat '{}' \; || true 43 | shell: bash 44 | 45 | - name: Upload test results 46 | if: failure() 47 | uses: actions/upload-artifact@v3 48 | with: 49 | name: coverage-test-failures 50 | path: ${{ runner.temp }}/package 51 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.toptal.com/developers/gitignore/api/r 2 | # Edit at https://www.toptal.com/developers/gitignore?templates=r 3 | 4 | ### R ### 5 | # History files 6 | .Rhistory 7 | .Rapp.history 8 | 9 | # Session Data files 10 | .RData 11 | .RDataTmp 12 | 13 | # User-specific files 14 | .Ruserdata 15 | 16 | # Example code in package build process 17 | *-Ex.R 18 | 19 | # Output files from R CMD build 20 | /*.tar.gz 21 | 22 | # Output files from R CMD check 23 | /*.Rcheck/ 24 | 25 | # RStudio files 26 | .Rproj.user/ 27 | 28 | # produced vignettes 29 | vignettes/*.html 30 | vignettes/*.pdf 31 | 32 | # OAuth2 token, see https://github.com/hadley/httr/releases/tag/v0.3 33 | .httr-oauth 34 | 35 | # knitr and R markdown default cache directories 36 | *_cache/ 37 | /cache/ 38 | 39 | # Temporary files created by R markdown 40 | *.utf8.md 41 | *.knit.md 42 | 43 | # R Environment Variables 44 | .Renviron 45 | 46 | # pkgdown site 47 | docs/ 48 | 49 | # translation temp files 50 | po/*~ 51 | 52 | # RStudio Connect folder 53 | rsconnect/ 54 | 55 | ### R.Bookdown Stack ### 56 | # R package: bookdown caching files 57 | /*_files/ 58 | 59 | # End of https://www.toptal.com/developers/gitignore/api/r 60 | inst/doc 61 | /doc/ 62 | /Meta/ 63 | docs 64 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | We as members, contributors, and leaders pledge to make participation in our 6 | community a harassment-free experience for everyone, regardless of age, body 7 | size, visible or invisible disability, ethnicity, sex characteristics, gender 8 | identity and expression, level of experience, education, socio-economic status, 9 | nationality, personal appearance, race, caste, color, religion, or sexual 10 | identity and orientation. 11 | 12 | We pledge to act and interact in ways that contribute to an open, welcoming, 13 | diverse, inclusive, and healthy community. 14 | 15 | ## Our Standards 16 | 17 | Examples of behavior that contributes to a positive environment for our 18 | community include: 19 | 20 | * Demonstrating empathy and kindness toward other people 21 | * Being respectful of differing opinions, viewpoints, and experiences 22 | * Giving and gracefully accepting constructive feedback 23 | * Accepting responsibility and apologizing to those affected by our mistakes, 24 | and learning from the experience 25 | * Focusing on what is best not just for us as individuals, but for the overall 26 | community 27 | 28 | Examples of unacceptable behavior include: 29 | 30 | * The use of sexualized language or imagery, and sexual attention or advances of 31 | any kind 32 | * Trolling, insulting or derogatory comments, and personal or political attacks 33 | * Public or private harassment 34 | * Publishing others' private information, such as a physical or email address, 35 | without their explicit permission 36 | * Other conduct which could reasonably be considered inappropriate in a 37 | professional setting 38 | 39 | ## Enforcement Responsibilities 40 | 41 | Community leaders are responsible for clarifying and enforcing our standards of 42 | acceptable behavior and will take appropriate and fair corrective action in 43 | response to any behavior that they deem inappropriate, threatening, offensive, 44 | or harmful. 45 | 46 | Community leaders have the right and responsibility to remove, edit, or reject 47 | comments, commits, code, wiki edits, issues, and other contributions that are 48 | not aligned to this Code of Conduct, and will communicate reasons for moderation 49 | decisions when appropriate. 50 | 51 | ## Scope 52 | 53 | This Code of Conduct applies within all community spaces, and also applies when 54 | an individual is officially representing the community in public spaces. 55 | Examples of representing our community include using an official e-mail address, 56 | posting via an official social media account, or acting as an appointed 57 | representative at an online or offline event. 58 | 59 | ## Enforcement 60 | 61 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 62 | reported to the community leaders responsible for enforcement at teofiln@gmail.com. 63 | All complaints will be reviewed and investigated promptly and fairly. 64 | 65 | All community leaders are obligated to respect the privacy and security of the 66 | reporter of any incident. 67 | 68 | ## Enforcement Guidelines 69 | 70 | Community leaders will follow these Community Impact Guidelines in determining 71 | the consequences for any action they deem in violation of this Code of Conduct: 72 | 73 | ### 1. Correction 74 | 75 | **Community Impact**: Use of inappropriate language or other behavior deemed 76 | unprofessional or unwelcome in the community. 77 | 78 | **Consequence**: A private, written warning from community leaders, providing 79 | clarity around the nature of the violation and an explanation of why the 80 | behavior was inappropriate. A public apology may be requested. 81 | 82 | ### 2. Warning 83 | 84 | **Community Impact**: A violation through a single incident or series of 85 | actions. 86 | 87 | **Consequence**: A warning with consequences for continued behavior. No 88 | interaction with the people involved, including unsolicited interaction with 89 | those enforcing the Code of Conduct, for a specified period of time. This 90 | includes avoiding interactions in community spaces as well as external channels 91 | like social media. Violating these terms may lead to a temporary or permanent 92 | ban. 93 | 94 | ### 3. Temporary Ban 95 | 96 | **Community Impact**: A serious violation of community standards, including 97 | sustained inappropriate behavior. 98 | 99 | **Consequence**: A temporary ban from any sort of interaction or public 100 | communication with the community for a specified period of time. No public or 101 | private interaction with the people involved, including unsolicited interaction 102 | with those enforcing the Code of Conduct, is allowed during this period. 103 | Violating these terms may lead to a permanent ban. 104 | 105 | ### 4. Permanent Ban 106 | 107 | **Community Impact**: Demonstrating a pattern of violation of community 108 | standards, including sustained inappropriate behavior, harassment of an 109 | individual, or aggression toward or disparagement of classes of individuals. 110 | 111 | **Consequence**: A permanent ban from any sort of public interaction within the 112 | community. 113 | 114 | ## Attribution 115 | 116 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 117 | version 2.1, available at 118 | . 119 | 120 | Community Impact Guidelines were inspired by 121 | [Mozilla's code of conduct enforcement ladder][https://github.com/mozilla/inclusion]. 122 | 123 | For answers to common questions about this code of conduct, see the FAQ at 124 | . Translations are available at . 125 | 126 | [homepage]: https://www.contributor-covenant.org 127 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: r2lambda 2 | Type: Package 3 | Title: Deploy R Scripts as AWS Lambda Functions 4 | Version: 0.1.0 5 | Authors@R: 6 | c(person(given = "Teofil", 7 | family = "Nakov", 8 | role = c("aut", "cre"), 9 | email = "teofiln@gmail.com") 10 | ) 11 | Description: The goal of `{r2lambda}` is to make it easier to go from an `R` script 12 | to a deployed AWS Lambda function. It does this by wrapping preexisting functionality 13 | from the `{paws}` and `{lambdr}` packages. 14 | License: GPL (>= 3) 15 | Encoding: UTF-8 16 | LazyData: true 17 | Imports: 18 | checkmate, 19 | glue, 20 | jsonlite, 21 | lambdr, 22 | logger, 23 | paws, 24 | rlang, 25 | stevedore, 26 | sys, 27 | uuid 28 | Remotes: richfitz/stevedore 29 | RoxygenNote: 7.3.2 30 | Suggests: 31 | covr, 32 | knitr, 33 | rmarkdown, 34 | testthat (>= 3.0.0) 35 | Config/testthat/edition: 3 36 | VignetteBuilder: knitr 37 | URL: https://discindo.github.io/r2lambda/ 38 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | GNU General Public License 2 | ========================== 3 | 4 | _Version 3, 29 June 2007_ 5 | _Copyright © 2007 Free Software Foundation, Inc. <>_ 6 | 7 | Everyone is permitted to copy and distribute verbatim copies of this license 8 | document, but changing it is not allowed. 9 | 10 | ## Preamble 11 | 12 | The GNU General Public License is a free, copyleft license for software and other 13 | kinds of works. 14 | 15 | The licenses for most software and other practical works are designed to take away 16 | your freedom to share and change the works. By contrast, the GNU General Public 17 | License is intended to guarantee your freedom to share and change all versions of a 18 | program--to make sure it remains free software for all its users. We, the Free 19 | Software Foundation, use the GNU General Public License for most of our software; it 20 | applies also to any other work released this way by its authors. You can apply it to 21 | your programs, too. 22 | 23 | When we speak of free software, we are referring to freedom, not price. Our General 24 | Public Licenses are designed to make sure that you have the freedom to distribute 25 | copies of free software (and charge for them if you wish), that you receive source 26 | code or can get it if you want it, that you can change the software or use pieces of 27 | it in new free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you these rights or 30 | asking you to surrender the rights. Therefore, you have certain responsibilities if 31 | you distribute copies of the software, or if you modify it: responsibilities to 32 | respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether gratis or for a fee, 35 | you must pass on to the recipients the same freedoms that you received. You must make 36 | sure that they, too, receive or can get the source code. And you must show them these 37 | terms so they know their rights. 38 | 39 | Developers that use the GNU GPL protect your rights with two steps: **(1)** assert 40 | copyright on the software, and **(2)** offer you this License giving you legal permission 41 | to copy, distribute and/or modify it. 42 | 43 | For the developers' and authors' protection, the GPL clearly explains that there is 44 | no warranty for this free software. For both users' and authors' sake, the GPL 45 | requires that modified versions be marked as changed, so that their problems will not 46 | be attributed erroneously to authors of previous versions. 47 | 48 | Some devices are designed to deny users access to install or run modified versions of 49 | the software inside them, although the manufacturer can do so. This is fundamentally 50 | incompatible with the aim of protecting users' freedom to change the software. The 51 | systematic pattern of such abuse occurs in the area of products for individuals to 52 | use, which is precisely where it is most unacceptable. Therefore, we have designed 53 | this version of the GPL to prohibit the practice for those products. If such problems 54 | arise substantially in other domains, we stand ready to extend this provision to 55 | those domains in future versions of the GPL, as needed to protect the freedom of 56 | users. 57 | 58 | Finally, every program is threatened constantly by software patents. States should 59 | not allow patents to restrict development and use of software on general-purpose 60 | computers, but in those that do, we wish to avoid the special danger that patents 61 | applied to a free program could make it effectively proprietary. To prevent this, the 62 | GPL assures that patents cannot be used to render the program non-free. 63 | 64 | The precise terms and conditions for copying, distribution and modification follow. 65 | 66 | ## TERMS AND CONDITIONS 67 | 68 | ### 0. Definitions 69 | 70 | “This License” refers to version 3 of the GNU General Public License. 71 | 72 | “Copyright” also means copyright-like laws that apply to other kinds of 73 | works, such as semiconductor masks. 74 | 75 | “The Program” refers to any copyrightable work licensed under this 76 | License. Each licensee is addressed as “you”. “Licensees” and 77 | “recipients” may be individuals or organizations. 78 | 79 | To “modify” a work means to copy from or adapt all or part of the work in 80 | a fashion requiring copyright permission, other than the making of an exact copy. The 81 | resulting work is called a “modified version” of the earlier work or a 82 | work “based on” the earlier work. 83 | 84 | A “covered work” means either the unmodified Program or a work based on 85 | the Program. 86 | 87 | To “propagate” a work means to do anything with it that, without 88 | permission, would make you directly or secondarily liable for infringement under 89 | applicable copyright law, except executing it on a computer or modifying a private 90 | copy. Propagation includes copying, distribution (with or without modification), 91 | making available to the public, and in some countries other activities as well. 92 | 93 | To “convey” a work means any kind of propagation that enables other 94 | parties to make or receive copies. Mere interaction with a user through a computer 95 | network, with no transfer of a copy, is not conveying. 96 | 97 | An interactive user interface displays “Appropriate Legal Notices” to the 98 | extent that it includes a convenient and prominently visible feature that **(1)** 99 | displays an appropriate copyright notice, and **(2)** tells the user that there is no 100 | warranty for the work (except to the extent that warranties are provided), that 101 | licensees may convey the work under this License, and how to view a copy of this 102 | License. If the interface presents a list of user commands or options, such as a 103 | menu, a prominent item in the list meets this criterion. 104 | 105 | ### 1. Source Code 106 | 107 | The “source code” for a work means the preferred form of the work for 108 | making modifications to it. “Object code” means any non-source form of a 109 | work. 110 | 111 | A “Standard Interface” means an interface that either is an official 112 | standard defined by a recognized standards body, or, in the case of interfaces 113 | specified for a particular programming language, one that is widely used among 114 | developers working in that language. 115 | 116 | The “System Libraries” of an executable work include anything, other than 117 | the work as a whole, that **(a)** is included in the normal form of packaging a Major 118 | Component, but which is not part of that Major Component, and **(b)** serves only to 119 | enable use of the work with that Major Component, or to implement a Standard 120 | Interface for which an implementation is available to the public in source code form. 121 | A “Major Component”, in this context, means a major essential component 122 | (kernel, window system, and so on) of the specific operating system (if any) on which 123 | the executable work runs, or a compiler used to produce the work, or an object code 124 | interpreter used to run it. 125 | 126 | The “Corresponding Source” for a work in object code form means all the 127 | source code needed to generate, install, and (for an executable work) run the object 128 | code and to modify the work, including scripts to control those activities. However, 129 | it does not include the work's System Libraries, or general-purpose tools or 130 | generally available free programs which are used unmodified in performing those 131 | activities but which are not part of the work. For example, Corresponding Source 132 | includes interface definition files associated with source files for the work, and 133 | the source code for shared libraries and dynamically linked subprograms that the work 134 | is specifically designed to require, such as by intimate data communication or 135 | control flow between those subprograms and other parts of the work. 136 | 137 | The Corresponding Source need not include anything that users can regenerate 138 | automatically from other parts of the Corresponding Source. 139 | 140 | The Corresponding Source for a work in source code form is that same work. 141 | 142 | ### 2. Basic Permissions 143 | 144 | All rights granted under this License are granted for the term of copyright on the 145 | Program, and are irrevocable provided the stated conditions are met. This License 146 | explicitly affirms your unlimited permission to run the unmodified Program. The 147 | output from running a covered work is covered by this License only if the output, 148 | given its content, constitutes a covered work. This License acknowledges your rights 149 | of fair use or other equivalent, as provided by copyright law. 150 | 151 | You may make, run and propagate covered works that you do not convey, without 152 | conditions so long as your license otherwise remains in force. You may convey covered 153 | works to others for the sole purpose of having them make modifications exclusively 154 | for you, or provide you with facilities for running those works, provided that you 155 | comply with the terms of this License in conveying all material for which you do not 156 | control copyright. Those thus making or running the covered works for you must do so 157 | exclusively on your behalf, under your direction and control, on terms that prohibit 158 | them from making any copies of your copyrighted material outside their relationship 159 | with you. 160 | 161 | Conveying under any other circumstances is permitted solely under the conditions 162 | stated below. Sublicensing is not allowed; section 10 makes it unnecessary. 163 | 164 | ### 3. Protecting Users' Legal Rights From Anti-Circumvention Law 165 | 166 | No covered work shall be deemed part of an effective technological measure under any 167 | applicable law fulfilling obligations under article 11 of the WIPO copyright treaty 168 | adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention 169 | of such measures. 170 | 171 | When you convey a covered work, you waive any legal power to forbid circumvention of 172 | technological measures to the extent such circumvention is effected by exercising 173 | rights under this License with respect to the covered work, and you disclaim any 174 | intention to limit operation or modification of the work as a means of enforcing, 175 | against the work's users, your or third parties' legal rights to forbid circumvention 176 | of technological measures. 177 | 178 | ### 4. Conveying Verbatim Copies 179 | 180 | You may convey verbatim copies of the Program's source code as you receive it, in any 181 | medium, provided that you conspicuously and appropriately publish on each copy an 182 | appropriate copyright notice; keep intact all notices stating that this License and 183 | any non-permissive terms added in accord with section 7 apply to the code; keep 184 | intact all notices of the absence of any warranty; and give all recipients a copy of 185 | this License along with the Program. 186 | 187 | You may charge any price or no price for each copy that you convey, and you may offer 188 | support or warranty protection for a fee. 189 | 190 | ### 5. Conveying Modified Source Versions 191 | 192 | You may convey a work based on the Program, or the modifications to produce it from 193 | the Program, in the form of source code under the terms of section 4, provided that 194 | you also meet all of these conditions: 195 | 196 | * **a)** The work must carry prominent notices stating that you modified it, and giving a 197 | relevant date. 198 | * **b)** The work must carry prominent notices stating that it is released under this 199 | License and any conditions added under section 7. This requirement modifies the 200 | requirement in section 4 to “keep intact all notices”. 201 | * **c)** You must license the entire work, as a whole, under this License to anyone who 202 | comes into possession of a copy. This License will therefore apply, along with any 203 | applicable section 7 additional terms, to the whole of the work, and all its parts, 204 | regardless of how they are packaged. This License gives no permission to license the 205 | work in any other way, but it does not invalidate such permission if you have 206 | separately received it. 207 | * **d)** If the work has interactive user interfaces, each must display Appropriate Legal 208 | Notices; however, if the Program has interactive interfaces that do not display 209 | Appropriate Legal Notices, your work need not make them do so. 210 | 211 | A compilation of a covered work with other separate and independent works, which are 212 | not by their nature extensions of the covered work, and which are not combined with 213 | it such as to form a larger program, in or on a volume of a storage or distribution 214 | medium, is called an “aggregate” if the compilation and its resulting 215 | copyright are not used to limit the access or legal rights of the compilation's users 216 | beyond what the individual works permit. Inclusion of a covered work in an aggregate 217 | does not cause this License to apply to the other parts of the aggregate. 218 | 219 | ### 6. Conveying Non-Source Forms 220 | 221 | You may convey a covered work in object code form under the terms of sections 4 and 222 | 5, provided that you also convey the machine-readable Corresponding Source under the 223 | terms of this License, in one of these ways: 224 | 225 | * **a)** Convey the object code in, or embodied in, a physical product (including a 226 | physical distribution medium), accompanied by the Corresponding Source fixed on a 227 | durable physical medium customarily used for software interchange. 228 | * **b)** Convey the object code in, or embodied in, a physical product (including a 229 | physical distribution medium), accompanied by a written offer, valid for at least 230 | three years and valid for as long as you offer spare parts or customer support for 231 | that product model, to give anyone who possesses the object code either **(1)** a copy of 232 | the Corresponding Source for all the software in the product that is covered by this 233 | License, on a durable physical medium customarily used for software interchange, for 234 | a price no more than your reasonable cost of physically performing this conveying of 235 | source, or **(2)** access to copy the Corresponding Source from a network server at no 236 | charge. 237 | * **c)** Convey individual copies of the object code with a copy of the written offer to 238 | provide the Corresponding Source. This alternative is allowed only occasionally and 239 | noncommercially, and only if you received the object code with such an offer, in 240 | accord with subsection 6b. 241 | * **d)** Convey the object code by offering access from a designated place (gratis or for 242 | a charge), and offer equivalent access to the Corresponding Source in the same way 243 | through the same place at no further charge. You need not require recipients to copy 244 | the Corresponding Source along with the object code. If the place to copy the object 245 | code is a network server, the Corresponding Source may be on a different server 246 | (operated by you or a third party) that supports equivalent copying facilities, 247 | provided you maintain clear directions next to the object code saying where to find 248 | the Corresponding Source. Regardless of what server hosts the Corresponding Source, 249 | you remain obligated to ensure that it is available for as long as needed to satisfy 250 | these requirements. 251 | * **e)** Convey the object code using peer-to-peer transmission, provided you inform 252 | other peers where the object code and Corresponding Source of the work are being 253 | offered to the general public at no charge under subsection 6d. 254 | 255 | A separable portion of the object code, whose source code is excluded from the 256 | Corresponding Source as a System Library, need not be included in conveying the 257 | object code work. 258 | 259 | A “User Product” is either **(1)** a “consumer product”, which 260 | means any tangible personal property which is normally used for personal, family, or 261 | household purposes, or **(2)** anything designed or sold for incorporation into a 262 | dwelling. In determining whether a product is a consumer product, doubtful cases 263 | shall be resolved in favor of coverage. For a particular product received by a 264 | particular user, “normally used” refers to a typical or common use of 265 | that class of product, regardless of the status of the particular user or of the way 266 | in which the particular user actually uses, or expects or is expected to use, the 267 | product. A product is a consumer product regardless of whether the product has 268 | substantial commercial, industrial or non-consumer uses, unless such uses represent 269 | the only significant mode of use of the product. 270 | 271 | “Installation Information” for a User Product means any methods, 272 | procedures, authorization keys, or other information required to install and execute 273 | modified versions of a covered work in that User Product from a modified version of 274 | its Corresponding Source. The information must suffice to ensure that the continued 275 | functioning of the modified object code is in no case prevented or interfered with 276 | solely because modification has been made. 277 | 278 | If you convey an object code work under this section in, or with, or specifically for 279 | use in, a User Product, and the conveying occurs as part of a transaction in which 280 | the right of possession and use of the User Product is transferred to the recipient 281 | in perpetuity or for a fixed term (regardless of how the transaction is 282 | characterized), the Corresponding Source conveyed under this section must be 283 | accompanied by the Installation Information. But this requirement does not apply if 284 | neither you nor any third party retains the ability to install modified object code 285 | on the User Product (for example, the work has been installed in ROM). 286 | 287 | The requirement to provide Installation Information does not include a requirement to 288 | continue to provide support service, warranty, or updates for a work that has been 289 | modified or installed by the recipient, or for the User Product in which it has been 290 | modified or installed. Access to a network may be denied when the modification itself 291 | materially and adversely affects the operation of the network or violates the rules 292 | and protocols for communication across the network. 293 | 294 | Corresponding Source conveyed, and Installation Information provided, in accord with 295 | this section must be in a format that is publicly documented (and with an 296 | implementation available to the public in source code form), and must require no 297 | special password or key for unpacking, reading or copying. 298 | 299 | ### 7. Additional Terms 300 | 301 | “Additional permissions” are terms that supplement the terms of this 302 | License by making exceptions from one or more of its conditions. Additional 303 | permissions that are applicable to the entire Program shall be treated as though they 304 | were included in this License, to the extent that they are valid under applicable 305 | law. If additional permissions apply only to part of the Program, that part may be 306 | used separately under those permissions, but the entire Program remains governed by 307 | this License without regard to the additional permissions. 308 | 309 | When you convey a copy of a covered work, you may at your option remove any 310 | additional permissions from that copy, or from any part of it. (Additional 311 | permissions may be written to require their own removal in certain cases when you 312 | modify the work.) You may place additional permissions on material, added by you to a 313 | covered work, for which you have or can give appropriate copyright permission. 314 | 315 | Notwithstanding any other provision of this License, for material you add to a 316 | covered work, you may (if authorized by the copyright holders of that material) 317 | supplement the terms of this License with terms: 318 | 319 | * **a)** Disclaiming warranty or limiting liability differently from the terms of 320 | sections 15 and 16 of this License; or 321 | * **b)** Requiring preservation of specified reasonable legal notices or author 322 | attributions in that material or in the Appropriate Legal Notices displayed by works 323 | containing it; or 324 | * **c)** Prohibiting misrepresentation of the origin of that material, or requiring that 325 | modified versions of such material be marked in reasonable ways as different from the 326 | original version; or 327 | * **d)** Limiting the use for publicity purposes of names of licensors or authors of the 328 | material; or 329 | * **e)** Declining to grant rights under trademark law for use of some trade names, 330 | trademarks, or service marks; or 331 | * **f)** Requiring indemnification of licensors and authors of that material by anyone 332 | who conveys the material (or modified versions of it) with contractual assumptions of 333 | liability to the recipient, for any liability that these contractual assumptions 334 | directly impose on those licensors and authors. 335 | 336 | All other non-permissive additional terms are considered “further 337 | restrictions” within the meaning of section 10. If the Program as you received 338 | it, or any part of it, contains a notice stating that it is governed by this License 339 | along with a term that is a further restriction, you may remove that term. If a 340 | license document contains a further restriction but permits relicensing or conveying 341 | under this License, you may add to a covered work material governed by the terms of 342 | that license document, provided that the further restriction does not survive such 343 | relicensing or conveying. 344 | 345 | If you add terms to a covered work in accord with this section, you must place, in 346 | the relevant source files, a statement of the additional terms that apply to those 347 | files, or a notice indicating where to find the applicable terms. 348 | 349 | Additional terms, permissive or non-permissive, may be stated in the form of a 350 | separately written license, or stated as exceptions; the above requirements apply 351 | either way. 352 | 353 | ### 8. Termination 354 | 355 | You may not propagate or modify a covered work except as expressly provided under 356 | this License. Any attempt otherwise to propagate or modify it is void, and will 357 | automatically terminate your rights under this License (including any patent licenses 358 | granted under the third paragraph of section 11). 359 | 360 | However, if you cease all violation of this License, then your license from a 361 | particular copyright holder is reinstated **(a)** provisionally, unless and until the 362 | copyright holder explicitly and finally terminates your license, and **(b)** permanently, 363 | if the copyright holder fails to notify you of the violation by some reasonable means 364 | prior to 60 days after the cessation. 365 | 366 | Moreover, your license from a particular copyright holder is reinstated permanently 367 | if the copyright holder notifies you of the violation by some reasonable means, this 368 | is the first time you have received notice of violation of this License (for any 369 | work) from that copyright holder, and you cure the violation prior to 30 days after 370 | your receipt of the notice. 371 | 372 | Termination of your rights under this section does not terminate the licenses of 373 | parties who have received copies or rights from you under this License. If your 374 | rights have been terminated and not permanently reinstated, you do not qualify to 375 | receive new licenses for the same material under section 10. 376 | 377 | ### 9. Acceptance Not Required for Having Copies 378 | 379 | You are not required to accept this License in order to receive or run a copy of the 380 | Program. Ancillary propagation of a covered work occurring solely as a consequence of 381 | using peer-to-peer transmission to receive a copy likewise does not require 382 | acceptance. However, nothing other than this License grants you permission to 383 | propagate or modify any covered work. These actions infringe copyright if you do not 384 | accept this License. Therefore, by modifying or propagating a covered work, you 385 | indicate your acceptance of this License to do so. 386 | 387 | ### 10. Automatic Licensing of Downstream Recipients 388 | 389 | Each time you convey a covered work, the recipient automatically receives a license 390 | from the original licensors, to run, modify and propagate that work, subject to this 391 | License. You are not responsible for enforcing compliance by third parties with this 392 | License. 393 | 394 | An “entity transaction” is a transaction transferring control of an 395 | organization, or substantially all assets of one, or subdividing an organization, or 396 | merging organizations. If propagation of a covered work results from an entity 397 | transaction, each party to that transaction who receives a copy of the work also 398 | receives whatever licenses to the work the party's predecessor in interest had or 399 | could give under the previous paragraph, plus a right to possession of the 400 | Corresponding Source of the work from the predecessor in interest, if the predecessor 401 | has it or can get it with reasonable efforts. 402 | 403 | You may not impose any further restrictions on the exercise of the rights granted or 404 | affirmed under this License. For example, you may not impose a license fee, royalty, 405 | or other charge for exercise of rights granted under this License, and you may not 406 | initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging 407 | that any patent claim is infringed by making, using, selling, offering for sale, or 408 | importing the Program or any portion of it. 409 | 410 | ### 11. Patents 411 | 412 | A “contributor” is a copyright holder who authorizes use under this 413 | License of the Program or a work on which the Program is based. The work thus 414 | licensed is called the contributor's “contributor version”. 415 | 416 | A contributor's “essential patent claims” are all patent claims owned or 417 | controlled by the contributor, whether already acquired or hereafter acquired, that 418 | would be infringed by some manner, permitted by this License, of making, using, or 419 | selling its contributor version, but do not include claims that would be infringed 420 | only as a consequence of further modification of the contributor version. For 421 | purposes of this definition, “control” includes the right to grant patent 422 | sublicenses in a manner consistent with the requirements of this License. 423 | 424 | Each contributor grants you a non-exclusive, worldwide, royalty-free patent license 425 | under the contributor's essential patent claims, to make, use, sell, offer for sale, 426 | import and otherwise run, modify and propagate the contents of its contributor 427 | version. 428 | 429 | In the following three paragraphs, a “patent license” is any express 430 | agreement or commitment, however denominated, not to enforce a patent (such as an 431 | express permission to practice a patent or covenant not to sue for patent 432 | infringement). To “grant” such a patent license to a party means to make 433 | such an agreement or commitment not to enforce a patent against the party. 434 | 435 | If you convey a covered work, knowingly relying on a patent license, and the 436 | Corresponding Source of the work is not available for anyone to copy, free of charge 437 | and under the terms of this License, through a publicly available network server or 438 | other readily accessible means, then you must either **(1)** cause the Corresponding 439 | Source to be so available, or **(2)** arrange to deprive yourself of the benefit of the 440 | patent license for this particular work, or **(3)** arrange, in a manner consistent with 441 | the requirements of this License, to extend the patent license to downstream 442 | recipients. “Knowingly relying” means you have actual knowledge that, but 443 | for the patent license, your conveying the covered work in a country, or your 444 | recipient's use of the covered work in a country, would infringe one or more 445 | identifiable patents in that country that you have reason to believe are valid. 446 | 447 | If, pursuant to or in connection with a single transaction or arrangement, you 448 | convey, or propagate by procuring conveyance of, a covered work, and grant a patent 449 | license to some of the parties receiving the covered work authorizing them to use, 450 | propagate, modify or convey a specific copy of the covered work, then the patent 451 | license you grant is automatically extended to all recipients of the covered work and 452 | works based on it. 453 | 454 | A patent license is “discriminatory” if it does not include within the 455 | scope of its coverage, prohibits the exercise of, or is conditioned on the 456 | non-exercise of one or more of the rights that are specifically granted under this 457 | License. You may not convey a covered work if you are a party to an arrangement with 458 | a third party that is in the business of distributing software, under which you make 459 | payment to the third party based on the extent of your activity of conveying the 460 | work, and under which the third party grants, to any of the parties who would receive 461 | the covered work from you, a discriminatory patent license **(a)** in connection with 462 | copies of the covered work conveyed by you (or copies made from those copies), or **(b)** 463 | primarily for and in connection with specific products or compilations that contain 464 | the covered work, unless you entered into that arrangement, or that patent license 465 | was granted, prior to 28 March 2007. 466 | 467 | Nothing in this License shall be construed as excluding or limiting any implied 468 | license or other defenses to infringement that may otherwise be available to you 469 | under applicable patent law. 470 | 471 | ### 12. No Surrender of Others' Freedom 472 | 473 | If conditions are imposed on you (whether by court order, agreement or otherwise) 474 | that contradict the conditions of this License, they do not excuse you from the 475 | conditions of this License. If you cannot convey a covered work so as to satisfy 476 | simultaneously your obligations under this License and any other pertinent 477 | obligations, then as a consequence you may not convey it at all. For example, if you 478 | agree to terms that obligate you to collect a royalty for further conveying from 479 | those to whom you convey the Program, the only way you could satisfy both those terms 480 | and this License would be to refrain entirely from conveying the Program. 481 | 482 | ### 13. Use with the GNU Affero General Public License 483 | 484 | Notwithstanding any other provision of this License, you have permission to link or 485 | combine any covered work with a work licensed under version 3 of the GNU Affero 486 | General Public License into a single combined work, and to convey the resulting work. 487 | The terms of this License will continue to apply to the part which is the covered 488 | work, but the special requirements of the GNU Affero General Public License, section 489 | 13, concerning interaction through a network will apply to the combination as such. 490 | 491 | ### 14. Revised Versions of this License 492 | 493 | The Free Software Foundation may publish revised and/or new versions of the GNU 494 | General Public License from time to time. Such new versions will be similar in spirit 495 | to the present version, but may differ in detail to address new problems or concerns. 496 | 497 | Each version is given a distinguishing version number. If the Program specifies that 498 | a certain numbered version of the GNU General Public License “or any later 499 | version” applies to it, you have the option of following the terms and 500 | conditions either of that numbered version or of any later version published by the 501 | Free Software Foundation. If the Program does not specify a version number of the GNU 502 | General Public License, you may choose any version ever published by the Free 503 | Software Foundation. 504 | 505 | If the Program specifies that a proxy can decide which future versions of the GNU 506 | General Public License can be used, that proxy's public statement of acceptance of a 507 | version permanently authorizes you to choose that version for the Program. 508 | 509 | Later license versions may give you additional or different permissions. However, no 510 | additional obligations are imposed on any author or copyright holder as a result of 511 | your choosing to follow a later version. 512 | 513 | ### 15. Disclaimer of Warranty 514 | 515 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 516 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 517 | PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER 518 | EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 519 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE 520 | QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE 521 | DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 522 | 523 | ### 16. Limitation of Liability 524 | 525 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY 526 | COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS 527 | PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, 528 | INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 529 | PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE 530 | OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE 531 | WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 532 | POSSIBILITY OF SUCH DAMAGES. 533 | 534 | ### 17. Interpretation of Sections 15 and 16 535 | 536 | If the disclaimer of warranty and limitation of liability provided above cannot be 537 | given local legal effect according to their terms, reviewing courts shall apply local 538 | law that most closely approximates an absolute waiver of all civil liability in 539 | connection with the Program, unless a warranty or assumption of liability accompanies 540 | a copy of the Program in return for a fee. 541 | 542 | _END OF TERMS AND CONDITIONS_ 543 | 544 | ## How to Apply These Terms to Your New Programs 545 | 546 | If you develop a new program, and you want it to be of the greatest possible use to 547 | the public, the best way to achieve this is to make it free software which everyone 548 | can redistribute and change under these terms. 549 | 550 | To do so, attach the following notices to the program. It is safest to attach them 551 | to the start of each source file to most effectively state the exclusion of warranty; 552 | and each file should have at least the “copyright” line and a pointer to 553 | where the full notice is found. 554 | 555 | 556 | Copyright (C) 557 | 558 | This program is free software: you can redistribute it and/or modify 559 | it under the terms of the GNU General Public License as published by 560 | the Free Software Foundation, either version 3 of the License, or 561 | (at your option) any later version. 562 | 563 | This program is distributed in the hope that it will be useful, 564 | but WITHOUT ANY WARRANTY; without even the implied warranty of 565 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 566 | GNU General Public License for more details. 567 | 568 | You should have received a copy of the GNU General Public License 569 | along with this program. If not, see . 570 | 571 | Also add information on how to contact you by electronic and paper mail. 572 | 573 | If the program does terminal interaction, make it output a short notice like this 574 | when it starts in an interactive mode: 575 | 576 | Copyright (C) 577 | This program comes with ABSOLUTELY NO WARRANTY; for details type 'show w'. 578 | This is free software, and you are welcome to redistribute it 579 | under certain conditions; type 'show c' for details. 580 | 581 | The hypothetical commands `show w` and `show c` should show the appropriate parts of 582 | the General Public License. Of course, your program's commands might be different; 583 | for a GUI interface, you would use an “about box”. 584 | 585 | You should also get your employer (if you work as a programmer) or school, if any, to 586 | sign a “copyright disclaimer” for the program, if necessary. For more 587 | information on this, and how to apply and follow the GNU GPL, see 588 | <>. 589 | 590 | The GNU General Public License does not permit incorporating your program into 591 | proprietary programs. If your program is a subroutine library, you may consider it 592 | more useful to permit linking proprietary applications with the library. If this is 593 | what you want to do, use the GNU Lesser General Public License instead of this 594 | License. But first, please read 595 | <>. 596 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(aws_connect) 4 | export(build_lambda) 5 | export(deploy_lambda) 6 | export(invoke_lambda) 7 | export(schedule_lambda) 8 | export(test_lambda) 9 | importFrom(glue,double_quote) 10 | importFrom(glue,glue) 11 | importFrom(glue,glue_collapse) 12 | importFrom(glue,single_quote) 13 | importFrom(paws,ecr) 14 | importFrom(paws,eventbridge) 15 | importFrom(paws,iam) 16 | importFrom(paws,lambda) 17 | importFrom(paws,s3) 18 | -------------------------------------------------------------------------------- /R/lambda-deploy.R: -------------------------------------------------------------------------------- 1 | #' build and tag lambda image locally 2 | #' 3 | #' @param tag A name for the Docker container and Lambda function 4 | #' @param runtime_function name of the runtime function 5 | #' @param runtime_path path to the script containing the runtime function 6 | #' @param support_path path to the support files (if any). Either NULL 7 | #' (the default) if all needed code is in the same `runtime_path` script, or a 8 | #' character vector of paths to additional files needed by the runtime script. 9 | #' @param renvlock_path path to the renv.lock file (if any). Default is NULL. 10 | #' @param dependencies list of dependencies (if any). Default is NULL. 11 | #' 12 | #' @details Use either `renvlock_path` or `dependencies` to install required 13 | #' packages, not both. By default, both are `NULL`, so the Docker image will 14 | #' have no additional packages installed. 15 | #' 16 | #' @importFrom glue glue glue_collapse single_quote double_quote 17 | #' @export 18 | build_lambda <- function( 19 | tag, runtime_function, runtime_path, support_path = NULL, 20 | renvlock_path = NULL, dependencies = NULL) { 21 | 22 | logger::log_info("[build_lambda] Checking system dependencies.") 23 | check_system_dependencies() 24 | 25 | logger::log_info("[build_lambda] Creating temporary working directory.") 26 | tdir <- tempdir() 27 | folder <- file.path(tdir, tag) 28 | 29 | logger::log_info("[build_lambda] Creating Dockerfile.") 30 | tryCatch( 31 | expr = { 32 | create_lambda_dockerfile( 33 | folder = folder, 34 | runtime_function = runtime_function, 35 | runtime_path = runtime_path, 36 | renvlock_path = renvlock_path, 37 | support_path = support_path, 38 | dependencies = dependencies 39 | ) 40 | }, 41 | error = function(e) { 42 | msg <- "Failed to create a folder with Lambda Dockerfile and runtime script." # nolint 43 | logger::log_error(msg) 44 | rlang::abort(e$message) 45 | } 46 | ) 47 | logger::log_warn( 48 | "[build_lambda] Created Dockerfile and lambda runtime script in temporary folder." # nolint 49 | ) 50 | 51 | logger::log_info("[build_lambda] Building Docker image.") 52 | tryCatch( 53 | expr = { 54 | create_lambda_image(folder = folder, tag = tag) 55 | }, 56 | error = function(e) { 57 | msg <- "Failed to create Lambda Docker image." 58 | logger::log_error(msg) 59 | rlang::abort(e$message) 60 | } 61 | ) 62 | logger::log_warn( 63 | "[build_lambda] Docker image built. This can take up substantial amount of disk space." # nolint 64 | ) 65 | logger::log_warn( 66 | "[build_lambda] Use `docker image ls` in your shell to see the image size." 67 | ) 68 | logger::log_warn( 69 | "[build_lambda] Use `docker rmi ` in your shell to remove an image." 70 | ) 71 | 72 | logger::log_success("[build_lambda] Done.") 73 | } 74 | 75 | #' test a lambda locally 76 | #' 77 | #' @param tag The tag of an existing local image tagged with ECR repo 78 | #' (see `build_lambda`) 79 | #' @param payload Named list. Arguments to lambda function. 80 | #' 81 | #' @examples 82 | #' \dontrun{ 83 | #' payload <- list(number = 2) 84 | #' tag <- "449283523352.dkr.ecr.us-east-1.amazonaws.com/myrepo51:latest" 85 | #' test_lambda(tag, payload) 86 | #' } 87 | #' @importFrom glue glue glue_collapse single_quote double_quote 88 | #' @export 89 | test_lambda <- function(tag, payload) { 90 | 91 | logger::log_info("[test_lambda] Converting payload list to json.") 92 | payload <- jsonlite::toJSON(payload, auto_unbox = TRUE) 93 | 94 | logger::log_info("[test_lambda] Starting docker client.") 95 | docker_cli <- stevedore::docker_client() 96 | 97 | logger::log_info("[test_lambda] Fetching remote tag.") 98 | repo_uri <- fetch_ecr_repo(tag) 99 | repo_tag <- paste0(repo_uri, ":latest") 100 | 101 | logger::log_info("[test_lambda] Checking image tag exists.") 102 | images <- docker_cli$image$list() 103 | tags <- images[["repo_tags"]] |> unlist() 104 | tag_exists <- repo_tag %in% tags 105 | 106 | if (!tag_exists) { 107 | msg <- glue("[test_lambda] Image tagged {repo_tag} not found.") 108 | logger::log_error(msg) 109 | message("Available images:\n", glue_collapse(sep = "\n", tags)) 110 | rlang::abort(msg) 111 | } 112 | 113 | uid <- uuid::UUIDgenerate(1) 114 | logger::log_info("[test_lambda] Starting lambda container with name {uid}.") 115 | docker_cli$container$run( 116 | image = repo_tag, 117 | port = "9000:8080", 118 | detach = TRUE, 119 | name = uid 120 | ) 121 | 122 | logger::log_info("[test_lambda] Invoking local lambda instance.") 123 | arg <- c( 124 | "-XPOST", 125 | "http://localhost:9000/2015-03-31/functions/function/invocations", 126 | "-d", 127 | payload 128 | ) 129 | response <- sys::exec_internal(cmd = "curl", args = arg) 130 | message("Response standard output:\n") 131 | response$stdout |> rawToChar() |> cat() 132 | cat("\n") 133 | message("Response standard error:\n") 134 | response$stderr |> rawToChar() |> cat() 135 | cat("\n") 136 | 137 | logger::log_info("[test_lambda] Stopping running lambda container.") 138 | running_containers <- docker_cli$container$list() 139 | to_stop <- running_containers$id[running_containers$name == uid] 140 | docker_cli$container$remove(id = to_stop, force = TRUE) 141 | 142 | logger::log_success("[test_lambda] Done.") 143 | invisible(response) 144 | } 145 | 146 | 147 | #' deploy a local lambda image to AWS Lambda 148 | #' 149 | #' @param tag The tag of an existing local image tagged with ECR repo 150 | #' (see `build_lambda`) 151 | #' @param set_aws_envvars logical, whether to set the local AWS secrets to the 152 | #' deployed Lambda environment (default = `FALSE`). This is useful if the 153 | #' Lambda needs to access other AWS service. When `TRUE`, the following 154 | #' envvars are set: `PROFILE`, `REGION`, `SECRET_ACCESS_KEY`, and 155 | #' `ACCESS_KEY_ID`. They are fetched using `Sys.getenv()`. 156 | #' @param ... Arguments passed onto `create_lambda_function` 157 | #' 158 | #' @examples 159 | #' \dontrun{ 160 | #' 161 | #' runtime_function <- "parity" 162 | #' runtime_path <- system.file("parity.R", package = "r2lambda") 163 | #' dependencies <- NULL 164 | #' 165 | #' build_lambda( 166 | #' tag = "myrepo52", 167 | #' runtime_function = runtime_function, 168 | #' runtime_path = runtime_path, 169 | #' dependencies = dependencies 170 | #' ) 171 | #' 172 | #' deploy_lambda(tag = "myrepo52") 173 | #' 174 | #' invoke_lambda( 175 | #' function_name = "myrepo52", 176 | #' payload = list(number = 3), 177 | #' invocation_type = "RequestResponse" 178 | #' ) 179 | #' 180 | #' } 181 | #' @importFrom paws ecr iam lambda s3 eventbridge 182 | #' @export 183 | deploy_lambda <- 184 | function(tag, set_aws_envvars = FALSE, ...) { 185 | ## Inputs are validated by lower-level functions 186 | 187 | logger::log_info( 188 | "[deploy_lambda] Pushing Docker image to AWS ECR. This may take a while." 189 | ) 190 | ecr_image_uri <- tryCatch( 191 | expr = { 192 | push_lambda_image(tag = tag) 193 | }, 194 | error = function(e) { 195 | msg <- "Failed to push Lambda Docker image to AWS ECR." 196 | logger::log_error(msg) 197 | rlang::abort(e$message) 198 | } 199 | ) 200 | 201 | logger::log_warn( 202 | "[deploy_lambda] Docker image pushed to ECR. This can take up substantial resources and incur cost." # nolint 203 | ) 204 | logger::log_warn( 205 | "[deploy_lambda] Use `paws::ecr()`, the AWS CLI, or the AWS console to manage your images." # nolint 206 | ) 207 | 208 | logger::log_info("[deploy_lambda] Creating Lambda role and basic policy.") 209 | iam_lambda_role <- tryCatch( 210 | expr = { 211 | create_lambda_exec_role(tag = tag) 212 | }, 213 | error = function(e) { 214 | msg <- "Failed to create Lambda execution role in AWS IAM." 215 | logger::log_error(msg) 216 | rlang::abort(e$message) 217 | } 218 | ) 219 | 220 | logger::log_warn( 221 | "[deploy_lambda] Created AWS role with basic lambda execution permissions." # nolint 222 | ) 223 | logger::log_warn( 224 | "[deploy_lambda] Use `paws::iam()`, the AWS CLI, or the AWS console to manage your roles, and permissions." # nolint 225 | ) 226 | 227 | ## TODO check if the role is OK before starting to create the lambda 228 | ## As is, we are waiting ten seconds before creating the lambda 229 | ## but this could be too long in some cases or not long enough in other 230 | Sys.sleep(10) 231 | 232 | logger::log_info("[deploy_lambda] Creating Lambda function from image.") 233 | lambda <- tryCatch( 234 | expr = { 235 | create_lambda_function( 236 | tag = tag, 237 | ecr_image_uri = ecr_image_uri, 238 | lambda_role_arn = iam_lambda_role$Role$Arn, 239 | set_aws_envvars = set_aws_envvars, 240 | ... 241 | ) 242 | }, 243 | error = function(e) { 244 | msg <- "Failed to push Lambda Docker image to AWS ECR." 245 | logger::log_error(msg) 246 | rlang::abort(msg) 247 | } 248 | ) 249 | logger::log_warn( 250 | "[deploy_lambda] Lambda function created. This can take up substantial resources and incur cost." # nolint 251 | ) 252 | logger::log_warn( 253 | "[deploy_lambda] Use `paws::lambda()`, the AWS CLI, or the AWS console to manage your functions." # nolint 254 | ) 255 | 256 | logger::log_warn("[deploy_lambda] Lambda function created successfully.") 257 | logger::log_warn( 258 | "[deploy_lambda] Pushed docker image to ECR with URI `{ecr_image_uri}`" 259 | ) 260 | logger::log_warn( 261 | "[deploy_lambda] Created Lambda execution role with ARN `{iam_lambda_role$Role$Arn}`" # nolint 262 | ) 263 | logger::log_warn( 264 | "[deploy_lambda] Created Lambda function `{lambda$FunctionName}` with ARN `{lambda$FunctionArn}`" # nolint 265 | ) 266 | 267 | logger::log_success("[deploy_lambda] Done.") 268 | 269 | invisible( 270 | list( 271 | ECR_image_uri = ecr_image_uri, 272 | IAM_lambda_role_arn = iam_lambda_role$Role$Arn, 273 | Lambda_function_arn = lambda$FunctionArn 274 | ) 275 | ) 276 | } 277 | 278 | #' invoke a lambda function 279 | #' @param function_name The name or arn of the function 280 | #' @param invocation_type One of ‘DryRun’, ‘RequestResponse’, 281 | #' or ‘Event’ see `?paws.compute::lambda_invoke` 282 | #' @param payload A named list internally converted to json 283 | #' @param include_logs logical, whether to show the lambda logs (default: FALSE) 284 | #' @examples 285 | #' \dontrun{ 286 | #' invoke_lambda( 287 | #' function_name = "parity", 288 | #' payload = list(number = 3), 289 | #' invocation_type = "RequestResponse" 290 | #' ) 291 | #' } 292 | #' @export 293 | invoke_lambda <- 294 | function(function_name, 295 | invocation_type, 296 | payload, 297 | include_logs = FALSE) { 298 | 299 | logger::log_info("[invoke_lambda] Validating inputs.") 300 | 301 | # assumes .Renviron is set up 302 | lambda_service <- aws_connect("lambda") 303 | 304 | logger::log_info("[invoke_lambda] Checking function state.") 305 | state <- lambda_service$get_function(FunctionName = function_name)$Configuration$State # nolint 306 | ## TODO: Should we also check `LastUpdateStatus` 307 | ## (https://docs.aws.amazon.com/lambda/latest/dg/functions-states.html) 308 | 309 | if (state == "Active" || state == "Inactive") { 310 | # if its inactive, ping it still, to activate it 311 | # it might fail to run, but the error message should clarify next steps 312 | logger::log_info("[invoke_lambda] Function state: {state}.") 313 | logger::log_info("[invoke_lambda] Invoking function.") 314 | response <- tryCatch( 315 | lambda_service$invoke( 316 | FunctionName = function_name, 317 | InvocationType = invocation_type, 318 | Payload = jsonlite::toJSON(payload), 319 | LogType = ifelse(include_logs, "Tail", "None") 320 | ), error = function(e) e$message) 321 | 322 | } else { 323 | logger::log_info( 324 | "[invoke_lambda] Failed to invoke the function due to {state} state." 325 | ) 326 | logger::log_info( 327 | "[invoke_lambda] Please try again if the reported state was `Pending`." 328 | ) 329 | } 330 | 331 | message("\nLambda response payload: ") 332 | response$Payload |> rawToChar() |> cat() 333 | cat("\n") 334 | 335 | if (include_logs) { 336 | message("\nLambda logs: ") 337 | jsonlite::base64_dec(response$LogResult) |> rawToChar() |> cat() 338 | } 339 | logger::log_success("[invoke_lambda] Done.") 340 | invisible(response) 341 | } 342 | 343 | #' Put a deployed lambda function on a schedule 344 | #' 345 | #' @param lambda_function character, the name (or tag) of the function. A check 346 | #' is done internally make sure the Lambda exists and fetch its ARN. 347 | #' @param execution_rate character, the rate to run the lambda function. Can use 348 | #' `rate` or `cron` specification. For details see the official documentation: 349 | #' https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-create-rule-schedule.html 350 | #' 351 | #' @examples 352 | #' \dontrun{ 353 | #' # Make Tidytuesday lambda fetch the Tidytuesday dataset every wednesday 354 | #' at 8 am schedule_lambda("Tidytuesday3", "cron(0 8 * * Wed)") 355 | #' } 356 | #' 357 | #' @export 358 | 359 | schedule_lambda <- function(lambda_function, execution_rate) { 360 | 361 | logger::log_info("[schedule_lambda] Validating inputs.") 362 | aws_lambda <- aws_connect("lambda") 363 | fun_list <- aws_lambda$list_functions() 364 | lambda_names <- sapply(fun_list$Functions, "[[", "FunctionName") 365 | 366 | if (!lambda_function %in% lambda_names) { 367 | msg <- glue( 368 | "[schedule_lambda] Cannot find deployed lambda function {lambda_function}" 369 | ) 370 | logger::log_error(msg) 371 | rlang::abort(msg) 372 | } 373 | 374 | logger::log_info( 375 | "[schedule_lambda] Found lambda function {lambda_function}. Fetching ARN." 376 | ) 377 | lambda_index <- which(lambda_function == lambda_names) 378 | lambda_function_arn <- fun_list$Functions[[lambda_index]]$FunctionArn 379 | 380 | rate_clean <- gsub("\\(|\\)| |\\*|\\?", "_", execution_rate) 381 | rule_name <- glue("schedule_rule_{rate_clean}_{lambda_function}") 382 | logger::log_info( 383 | "[schedule_lambda] Creating event schedule rule with name {rule_name}" 384 | ) 385 | 386 | rule_arn <- tryCatch( 387 | expr = { 388 | create_event_rule_for_schedule( 389 | rule_name = rule_name, rate = execution_rate 390 | ) 391 | }, 392 | error = function(e) { 393 | logger::log_error(e$message) 394 | rlang::abort(e$message) 395 | } 396 | ) 397 | 398 | event_name <- glue("schedule_event_{rate_clean}_{lambda_function}") 399 | logger::log_info( 400 | "[schedule_lambda] Adding permission to execute lambda to event with name {event_name}" # nolint 401 | ) 402 | tryCatch( 403 | expr = { 404 | lambda_add_permission_for_schedule( 405 | function_name = lambda_function, 406 | scheduled_event_name = event_name, 407 | scheduled_rule_arn = rule_arn 408 | ) 409 | }, 410 | error = function(e) { 411 | logger::log_error(e$message) 412 | rlang::abort(e$message) 413 | } 414 | ) 415 | 416 | logger::log_info( 417 | "[schedule_lambda] Adding lambda function {lambda_function} to eventbridge schedule." # nolint 418 | ) 419 | tryCatch( 420 | expr = { 421 | add_lambda_to_eventridge( 422 | rule_name = rule_name, 423 | lambda_function_arn = lambda_function_arn 424 | ) 425 | }, 426 | error = function(e) { 427 | logger::log_error(e$message) 428 | rlang::abort(e$message) 429 | } 430 | ) 431 | 432 | logger::log_info("[schedule_lambda] Done.") 433 | } 434 | -------------------------------------------------------------------------------- /R/lambda-util.R: -------------------------------------------------------------------------------- 1 | is_aws_cli_installed <- function() { 2 | which_aws <- Sys.which("aws") 3 | IS_AWS_INSTALLED <- ifelse(which_aws[[1]] == "", FALSE, TRUE) 4 | checkmate::assert_true(IS_AWS_INSTALLED) 5 | } 6 | 7 | is_docker_installed <- function() { 8 | which_docker <- Sys.which("docker") 9 | IS_DOCKER_INSTALLED <- ifelse(which_docker[[1]] == "", FALSE, TRUE) 10 | checkmate::assert_true(IS_DOCKER_INSTALLED) 11 | } 12 | 13 | #' check_system_deps 14 | #' @noRd 15 | check_system_dependencies <- function() { 16 | # logger::log_debug( 17 | # "[check_system_dependencies] Checking if `aws cli` is installed.") 18 | # is_aws_cli_installed() 19 | 20 | logger::log_debug( 21 | "[check_system_dependencies] Checking if `docker` is installed." 22 | ) 23 | is_docker_installed() 24 | 25 | logger::log_debug("[check_system_dependencies] Done.") 26 | } 27 | 28 | #' connect to an aws service 29 | #' @param service character, the name of a service, e.g., "lambda" or "iam". 30 | #' Should be a function exported by `paws` (see `getNamespaceExports("paws")`) 31 | #' @examples 32 | #' \dontrun{ 33 | #' aws_connect("lambda") 34 | #' } 35 | #' @importFrom glue glue glue_collapse single_quote double_quote 36 | #' @export 37 | aws_connect <- function(service) { 38 | logger::log_debug("[aws_connect] Checking requested service.") 39 | 40 | if (!service %in% getNamespaceExports("paws")) { 41 | msg <- glue( 42 | "The service `{service}` does not appear to be available in `paws`." 43 | ) 44 | logger::log_error(msg) 45 | rlang::abort(msg) 46 | } 47 | 48 | logger::log_debug("[aws_connect] Connecting to AWS.") 49 | 50 | .service <- utils::getFromNamespace(service, "paws") 51 | .service() 52 | } 53 | 54 | #' install_deps_line 55 | #' @noRd 56 | install_deps_line <- function(deps) { 57 | checkmate::assert_character(deps) 58 | 59 | repo <- single_quote( 60 | "https://packagemanager.rstudio.com/all/__linux__/centos7/latest" 61 | ) 62 | glued <- glue_collapse(single_quote(deps), sep = ", ") 63 | glue('RUN Rscript -e "install.packages(c({glued}), repos = {repo})"') 64 | } 65 | 66 | #' runtime_line 67 | #' @noRd 68 | runtime_line <- function(runtime) { 69 | checkmate::assert_character(runtime) 70 | rt <- double_quote(runtime) 71 | glue("CMD [{rt}]") 72 | } 73 | 74 | #' support script line 75 | #' @noRd 76 | support_line <- function(support) { 77 | checkmate::assert_character(support) 78 | glue("COPY {support} {support}") 79 | } 80 | 81 | #' renv_line 82 | #' @noRd 83 | renv_line <- function(renvlock_path) { 84 | checkmate::assert_character(renvlock_path) 85 | cmd1 <- glue("COPY {renvlock_path} renv.lock") 86 | cmd2 <- glue("RUN Rscript -e 'renv::restore()'") 87 | glue_collapse(c(cmd1, cmd2), sep = "\n") 88 | } 89 | 90 | #' parse password from ecr token 91 | #' @noRd 92 | parse_password <- function(ecr_token) { 93 | ecr_token |> 94 | jsonlite::base64_dec() |> 95 | rawToChar() |> 96 | gsub(pattern = "^AWS:", replacement = "") 97 | } 98 | 99 | #' create_lambda_dockerfile 100 | #' 101 | #' @param folder path to store the Dockerfile 102 | #' @param runtime_function name of the runtime function 103 | #' @param runtime_path path to the script containing the runtime function 104 | #' @param support_path path to the supporting code (if any). Can be `NULL` 105 | #' (default) or a character vector of paths to scripts. 106 | #' @param renvlock_path path to the renv.lock file (if any) 107 | #' @param dependencies list of dependencies (if any) 108 | #' 109 | #' @details Use either `renvlock_path` or `dependencies` to install required 110 | #' packages, not both. By default, both are `NULL`, so the Docker image will 111 | #' have no additional packages installed. 112 | #' 113 | #' @examples 114 | #' \dontrun{ 115 | #' runtime_function <- "parity" 116 | #' runtime_path <- system.file("parity.R", package = "r2lambda") 117 | #' folder <- "~/Desktop/parity-lambda" 118 | #' dependencies <- NULL 119 | #' 120 | #' create_lambda_dockerfile( 121 | #' folder = folder, 122 | #' runtime_function = runtime_function, 123 | #' runtime_path = runtime_path, 124 | #' dependencies = dependencies 125 | #' ) 126 | #' create_lambda_image(folder, tag = "test-tag") 127 | #' dir.exists(folder) 128 | #' dir(folder) 129 | #' unlink(folder, recursive = TRUE) 130 | #' } 131 | #' @noRd 132 | create_lambda_dockerfile <- 133 | function(folder, 134 | runtime_function, 135 | runtime_path, 136 | support_path = NULL, 137 | renvlock_path = NULL, 138 | dependencies = NULL) { 139 | logger::log_debug("[create_lambda_dockerfile] Validating inputs.") 140 | 141 | checkmate::assert_character( 142 | x = folder, 143 | min.chars = 1, 144 | len = 1 145 | ) 146 | 147 | if (checkmate::test_directory_exists(folder)) { 148 | msg <- glue( 149 | "[create_lambda_dockerfile] Directory {folder} exists. Please choose another name." # nolint 150 | ) 151 | logger::log_error(msg) 152 | rlang::abort(msg) 153 | } 154 | 155 | checkmate::assert_character( 156 | x = runtime_function, 157 | min.chars = 1, 158 | len = 1, 159 | any.missing = FALSE 160 | ) 161 | 162 | checkmate::assert_character( 163 | x = runtime_path, 164 | min.chars = 1, 165 | any.missing = FALSE 166 | ) 167 | 168 | if (!checkmate::test_file_exists(runtime_path)) { 169 | msg <- glue( 170 | "[create_lambda_dockerfile] Can't access runtime script {runtime_path}." 171 | ) 172 | logger::log_error(msg) 173 | rlang::abort(msg) 174 | } 175 | 176 | checkmate::assert_character( 177 | x = support_path, 178 | min.chars = 1, 179 | null.ok = TRUE 180 | ) 181 | 182 | if (!is.null(support_path)) { 183 | lapply(support_path, function(x) { 184 | if (!checkmate::test_file_exists(x)) { 185 | msg <- glue( 186 | "[create_lambda_dockerfile] Can't access support script {x}." 187 | ) 188 | logger::log_error(msg) 189 | rlang::abort(msg) 190 | } 191 | }) 192 | } 193 | 194 | checkmate::assert_character( 195 | x = dependencies, 196 | min.chars = 1, 197 | null.ok = TRUE 198 | ) 199 | 200 | checkmate::assert_character( 201 | x = renvlock_path, 202 | min.chars = 1, 203 | null.ok = TRUE 204 | ) 205 | 206 | if (!is.null(renvlock_path)) { 207 | if (!checkmate::test_file_exists(renvlock_path)) { 208 | msg <- glue( 209 | "[create_lambda_dockerfile] Can't access renv.lock file {renvlock_path}." # nolint 210 | ) 211 | logger::log_error(msg) 212 | rlang::abort(msg) 213 | } 214 | } 215 | 216 | # Can't use both dependencies and renvlock 217 | if (!is.null(renvlock_path) && !is.null(dependencies)) { 218 | msg <- "[create_lambda_dockerfile] Can't use both dependencies and renv." 219 | logger::log_error(msg) 220 | rlang::abort(msg) 221 | } 222 | 223 | logger::log_debug( 224 | "[create_lambda_dockerfile] Creating directory with Dockerfile and runtime script." # nolint 225 | ) 226 | 227 | docker_template <- system.file( 228 | "lambdr_dockerfile.txt", 229 | package = "r2lambda" 230 | ) 231 | 232 | dir.create(folder) 233 | dir.exists(folder) 234 | 235 | file.copy(runtime_path, folder) 236 | file.rename( 237 | from = file.path(folder, basename(runtime_path)), 238 | to = file.path(folder, "runtime.R") 239 | ) 240 | 241 | if (!is.null(support_path)) { 242 | lapply(support_path, function(x) { 243 | file.copy(x, folder) 244 | }) 245 | } 246 | 247 | file.copy(docker_template, folder, recursive = FALSE) 248 | file.rename( 249 | from = file.path(folder, basename(docker_template)), 250 | to = file.path(folder, "Dockerfile") 251 | ) 252 | 253 | logger::log_debug( 254 | "[create_lambda_dockerfile] Updating Dockerfile with dependencies and runtime info." # nolint 255 | ) 256 | 257 | if (!is.null(dependencies)) { 258 | deps_string <- install_deps_line(deps = c(dependencies)) 259 | write(deps_string, 260 | file = file.path(folder, "Dockerfile"), 261 | append = TRUE 262 | ) 263 | } 264 | 265 | if (!is.null(renvlock_path)) { 266 | file.copy(renvlock_path, folder) 267 | renv_string <- renv_line(basename(renvlock_path)) 268 | write(renv_string, 269 | file = file.path(folder, "Dockerfile"), 270 | append = TRUE 271 | ) 272 | } 273 | 274 | runtime_string <- runtime_line(runtime_function) 275 | 276 | write(runtime_string, 277 | file = file.path(folder, "Dockerfile"), 278 | append = TRUE 279 | ) 280 | 281 | if (!is.null(support_path)) { 282 | lapply(support_path, function(x) { 283 | support_string <- support_line(basename(x)) 284 | write(support_string, 285 | file = file.path(folder, "Dockerfile"), 286 | append = TRUE 287 | ) 288 | }) 289 | } 290 | 291 | logger::log_debug("[create_lambda_dockerfile] Done.") 292 | } 293 | 294 | #' Fetch or create ecr repo 295 | #' 296 | #' @param tag a tag for the image 297 | #' @noRd 298 | fetch_ecr_repo <- function(tag) { 299 | logger::log_debug("[fetch_ecr_repo] Checking if repository already exists.") 300 | ecr_service <- aws_connect("ecr") 301 | 302 | repos <- ecr_service$describe_repositories()$repositories 303 | repos_names <- sapply(repos, "[[", "repositoryName") 304 | 305 | if (tag %in% repos_names) { 306 | logger::log_debug("[fetch_ecr_repo] Repository exists. Fetching URI.") 307 | needed_repo <- repos_names == tag 308 | repo_uri <- repos[needed_repo][[1]]$repositoryUri 309 | } else { 310 | logger::log_debug( 311 | "[fetch_ecr_repo] Creating new repository and fetching URI." 312 | ) 313 | repo_meta <- ecr_service$create_repository( 314 | repositoryName = tag, 315 | imageScanningConfiguration = list(scanOnPush = TRUE) 316 | ) 317 | repo_uri <- repo_meta$repository$repositoryUri 318 | } 319 | logger::log_debug("[fetch_ecr_repo] Done.") 320 | return(repo_uri) 321 | } 322 | 323 | #' create_lambda_container 324 | #' 325 | #' @param folder path to the folder containing the lambda runtime 326 | #' script and Dockerfile 327 | #' @param tag a tag for the image 328 | #' @noRd 329 | create_lambda_image <- function(folder, tag) { 330 | logger::log_debug("[create_lambda_image] Validating inputs.") 331 | checkmate::assert_character(tag) 332 | checkmate::assert_character(folder) 333 | checkmate::assert_directory_exists(folder) 334 | 335 | checkmate::assert_file_exists(file.path(folder, "Dockerfile")) 336 | checkmate::assert_file_exists(file.path(folder, "runtime.R")) 337 | 338 | logger::log_debug( 339 | "[create_lambda_image] Confirming that files are not empty." 340 | ) 341 | checkmate::assert_numeric( 342 | x = file.size(file.path(folder, "Dockerfile")), 343 | lower = 1, 344 | any.missing = FALSE, 345 | all.missing = FALSE, 346 | len = 1, 347 | .var.name = "Check if file is empty." 348 | ) 349 | 350 | checkmate::assert_numeric( 351 | x = file.size(file.path(folder, "runtime.R")), 352 | lower = 1, 353 | any.missing = FALSE, 354 | all.missing = FALSE, 355 | len = 1, 356 | .var.name = "Check if file is empty." 357 | ) 358 | 359 | logger::log_debug("[create_lambda_image] Create or fetch a remote tag.") 360 | repo_uri <- fetch_ecr_repo(tag = tag) 361 | 362 | logger::log_debug("[create_lambda_image] Building docker image.") 363 | 364 | docker_cli <- stevedore::docker_client() 365 | docker_cli$image$build(context = folder, tag = repo_uri) 366 | 367 | logger::log_debug("[create_lambda_image] Done.") 368 | } 369 | 370 | #' push_lambda_container to AWS ECR 371 | #' 372 | #' @param tag the tag of an existing local image 373 | #' @noRd 374 | push_lambda_image <- function(tag) { 375 | logger::log_debug("[push_lambda_image] Validating inputs.") 376 | checkmate::assert_character(tag) 377 | 378 | logger::log_debug("[push_lambda_image] Authenticating Docker with AWS ECR.") 379 | 380 | ecr_service <- aws_connect("ecr") 381 | ecr_token <- ecr_service$get_authorization_token() 382 | ecr_password <- parse_password( 383 | ecr_token$authorizationData[[1]]$authorizationToken 384 | ) 385 | repo_uri <- fetch_ecr_repo(tag) 386 | server_address <- gsub(pattern = "/.*$", replacement = "", x = repo_uri) 387 | 388 | docker_cli <- stevedore::docker_client() 389 | docker_cli$login( 390 | username = "AWS", 391 | password = ecr_password, 392 | serveraddress = server_address 393 | ) 394 | 395 | logger::log_debug("[push_lambda_image] Pushing Docker image to AWS ECR.") 396 | 397 | tryCatch( 398 | expr = { 399 | docker_cli$image$push(name = repo_uri) 400 | }, 401 | error = function(e) { 402 | Sys.sleep(2) 403 | logger::log_debug( 404 | "[push_lambda_image] Pushing Docker image to AWS ECR. Second try." 405 | ) 406 | docker_cli$image$push(name = repo_uri) 407 | } 408 | ) 409 | 410 | logger::log_debug("[push_lambda_image] Done.") 411 | invisible(repo_uri) 412 | } 413 | 414 | #' create_lambda_exec_role 415 | #' @param tag the tag of an existing local image 416 | #' @noRd 417 | create_lambda_exec_role <- function(tag) { 418 | logger::log_debug("[create_lambda_exec_role] Validating inputs.") 419 | checkmate::assert_character(tag) 420 | 421 | logger::log_debug("[create_lambda_exec_role] Getting Lambda role json.") 422 | role <- system.file("lambda-role.json", package = "r2lambda") 423 | role_string <- lambdr::as_stringified_json(jsonlite::fromJSON(role)) 424 | role_name <- paste(tag, uuid::UUIDgenerate(1), sep = "--") 425 | 426 | logger::log_debug("[create_lambda_exec_role] Creating Lambda execution role.") 427 | iam_service <- aws_connect("iam") 428 | role_meta <- iam_service$create_role( 429 | RoleName = role_name, 430 | AssumeRolePolicyDocument = role_string 431 | ) 432 | 433 | logger::log_debug( 434 | "[create_lambda_exec_role] Attaching basic execution role policy." 435 | ) 436 | iam_service$attach_role_policy( 437 | RoleName = role_name, 438 | PolicyArn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" # nolint 439 | ) 440 | 441 | logger::log_debug("[create_lambda_exec_role] Done.") 442 | invisible(role_meta) 443 | } 444 | 445 | #' delete_lambda_exec_role 446 | #' @noRd 447 | delete_lambda_exec_role <- function(tag) { 448 | ## TODO: this probably won't work as there is a policy attached to the role 449 | iam_service <- aws_connect("iam") 450 | iam_service$delete_role( 451 | RoleName = tag 452 | ) 453 | } 454 | 455 | #' create_lambda_function 456 | #' 457 | #' @param tag the tag of an existing local image 458 | #' @param ecr_image_uri the URI of the image to use 459 | #' @param lambda_role_arn the arn of the execution role created for this lambda 460 | #' @param set_aws_envvars logical, whether to set the local AWS secrets to the 461 | #' deployed Lambda environment. This is useful if the Lambda needs to access 462 | #' other AWS service. When `TRUE`, the following envvars are set: `PROFILE`, 463 | #' `REGION`, `SECRET_ACCESS_KEY`, and `ACCESS_KEY_ID`. They are fetched using 464 | #' `Sys.getenv()`. 465 | #' 466 | #' @param ... arguments passed onto `paws.compute:::lambda_create_function()`. 467 | #' For example `Timeout` to increase the execution time, and `MemorySize` 468 | #' to request more memory. 469 | #' 470 | #' @noRd 471 | create_lambda_function <- 472 | function(tag, 473 | ecr_image_uri, 474 | lambda_role_arn, 475 | set_aws_envvars = FALSE, 476 | ...) { 477 | logger::log_debug("[create_lambda_function] Validating inputs.") 478 | checkmate::assert_character(tag) 479 | 480 | envvar_list <- list(Variables = list()) 481 | 482 | ## TODO: enable adding arbitrary envvars 483 | if (isTRUE(set_aws_envvars)) { 484 | envvar_list <- list( 485 | Variables = list( 486 | REGION = Sys.getenv("REGION"), 487 | PROFILE = Sys.getenv("PROFILE"), 488 | SECRET_ACCESS_KEY = Sys.getenv("SECRET_ACCESS_KEY"), 489 | ACCESS_KEY_ID = Sys.getenv("ACCESS_KEY_ID") 490 | ) 491 | ) 492 | } 493 | 494 | logger::log_debug("[create_lambda_function] Creating lambda function.") 495 | lambda_service <- aws_connect("lambda") 496 | lambda <- lambda_service$create_function( 497 | FunctionName = tag, 498 | Code = list(ImageUri = glue("{ecr_image_uri}:latest")), 499 | PackageType = "Image", 500 | Role = lambda_role_arn, 501 | Environment = envvar_list, 502 | ... 503 | ) 504 | 505 | logger::log_debug("[create_lambda_function] Done.") 506 | invisible(lambda) 507 | } 508 | 509 | #' delete_lambda_function 510 | #' @noRd 511 | delete_lambda_function <- function(tag) { 512 | lambda_service <- aws_connect("lambda") 513 | lambda_service$delete_function( 514 | FunctionName = tag 515 | ) 516 | } 517 | 518 | #' Create a rule for event schedule 519 | #' 520 | #' @param rule_name character, the name of the rule (used later when adding 521 | #' targets to the event) 522 | #' @param rate character, the rate at which we want the function to run, 523 | #' e.g., `5 minutes`, `1 minute` 524 | #' 525 | #' @noRd 526 | create_event_rule_for_schedule <- function(rule_name, rate) { 527 | logger::log_debug( 528 | "[create_event_rule_for_schedule] Creating rule for event schedule." 529 | ) 530 | 531 | aws_event <- aws_connect("eventbridge") 532 | 533 | rule <- tryCatch( 534 | expr = { 535 | aws_event$put_rule( 536 | Name = rule_name, 537 | ScheduleExpression = rate 538 | ) 539 | }, 540 | error = function(e) { 541 | logger::log_error(e$message) 542 | rlang::abort(e$message) 543 | } 544 | ) 545 | 546 | logger::log_debug("[create_event_rule_for_schedule] Done.") 547 | return(rule$RuleArn) 548 | } 549 | 550 | #' Add permission for event to invoke lambda 551 | #' 552 | #' @param function_name character, the name of the lambda function to schedule 553 | #' @param scheduled_event_name character, a name for our event 554 | #' @param scheduled_rule_arn character, the ARN of the scheduled event role (as 555 | #' returned from `create_event_rule_for_schedule`) 556 | #' 557 | #' @noRd 558 | lambda_add_permission_for_schedule <- 559 | function(function_name, 560 | scheduled_event_name, 561 | scheduled_rule_arn) { 562 | logger::log_debug("[lambda_add_permission_for_schedule] Adding permission for event schedule.") # nolint 563 | aws_lambda <- aws_connect("lambda") 564 | aws_lambda$add_permission( 565 | FunctionName = function_name, 566 | StatementId = scheduled_event_name, 567 | Action = "lambda:InvokeFunction", 568 | Principal = "events.amazonaws.com", 569 | SourceArn = scheduled_rule_arn 570 | ) 571 | logger::log_debug("[lambda_add_permission_for_schedule] Done.") 572 | } 573 | 574 | #' Add lambda to eventbridge rule 575 | #' 576 | #' @param rule_name character, the name of the rule 577 | #' @param lambda_function_arn character, the ARN of the lambda function 578 | #' 579 | #' @noRd 580 | add_lambda_to_eventridge <- 581 | function(rule_name, lambda_function_arn) { 582 | logger::log_debug( 583 | "[add_lambda_to_eventridge] Adding lambda function to events." 584 | ) 585 | aws_event <- aws_connect("eventbridge") 586 | aws_event$put_targets( 587 | Rule = rule_name, 588 | Targets = list(list(Id = 1, Arn = lambda_function_arn)) 589 | ) 590 | logger::log_debug("[add_lambda_to_eventridge] Done.") 591 | } 592 | -------------------------------------------------------------------------------- /R/s3.R: -------------------------------------------------------------------------------- 1 | #' Update policy template with target S3 bucket 2 | #' @param bucket_arn the ARN of an S3 bucket 3 | #' @noRd 4 | prepare_s3_put_policy <- function(bucket_arn) { 5 | role <- system.file("lambda-S3-put-role.json", package = "r2lambda") 6 | role_list <- jsonlite::fromJSON(role) 7 | role_list$Statement$Resource <- bucket_arn 8 | role_string <- lambdr::as_stringified_json(role_list) 9 | return(role_string) 10 | } 11 | 12 | grant_lambda_s3_put_access <- function(lambda, bucket) { 13 | # find lambda arn 14 | # find bucket arn 15 | # make policy 16 | # put it in AWS 17 | # attach it to a lambda 18 | } 19 | 20 | -------------------------------------------------------------------------------- /R/zzz.R: -------------------------------------------------------------------------------- 1 | .onLoad <- function(libname, pkgname) { 2 | utils::globalVariables(".") 3 | } 4 | -------------------------------------------------------------------------------- /README.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: github_document 3 | --- 4 | 5 | 6 | 7 | ```{r, include = FALSE} 8 | knitr::opts_chunk$set( 9 | collapse = TRUE, 10 | comment = "#>", 11 | fig.path = "man/figures/README-", 12 | out.width = "100%" 13 | ) 14 | ``` 15 | 16 | # r2lambda 17 | 18 | 19 | 20 | [![R-CMD-check](https://github.com/discindo/r2lambda/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/discindo/r2lambda/actions/workflows/R-CMD-check.yaml) 21 | [![Codecov test coverage](https://codecov.io/gh/discindo/r2lambda/branch/main/graph/badge.svg)](https://app.codecov.io/gh/discindo/r2lambda?branch=main) 22 | 23 | 24 | The goal of `{r2lambda}` is to make it easier to go from an `R` script to a deployed 25 | `AWS Lambda` function. 26 | 27 | ## Requirements 28 | 29 | - [docker](https://docs.docker.com/get-docker/) is required to build, tag, and push the image. 30 | 31 | ## Installation 32 | 33 | You can install the development version of `{r2lambda}` like so: 34 | 35 | ``` r 36 | # install_packages("remotes") 37 | remotes::install_github("discindo/r2lambda") 38 | ``` 39 | 40 | ## Setup 41 | 42 | `r2lambda` assumes credentials for connecting to AWS services are available 43 | in the `R` session. This can be done via an `.Renviron` file that should set 44 | enironmental variables like so: 45 | 46 | ``` 47 | AWS_ACCESS_KEY_ID = "YOUR AWS ACCESS KEY ID" 48 | AWS_SECRET_ACCESS_KEY = "YOUR AWS SECRET ACCESS KEY" 49 | AWS_PROFILE = "YOUR AWS PROFILE" 50 | AWS_REGION = "YOUR AWS REGION" 51 | ``` 52 | 53 | But since `r2lambda` uses `paws` under the hood, all authentication methods 54 | supported by `paws` are available in `r2lambda`. See 55 | [here](https://github.com/paws-r/paws/blob/main/docs/credentials.md) for details 56 | on setting credentials, region, profile, etc. 57 | 58 | ## Workflow 59 | 60 | ### Build a docker image for the lambda function 61 | 62 | ```{r, eval = FALSE} 63 | runtime_function <- "parity" 64 | runtime_path <- system.file("parity.R", package = "r2lambda") 65 | renvlock_path <- system.file("renv.lock", package = "r2lambda") 66 | dependencies <- NULL 67 | 68 | # Might take a while, its building a docker image 69 | build_lambda( 70 | tag = "parity1", 71 | runtime_function = runtime_function, 72 | runtime_path = runtime_path, 73 | renvlock_path = renvlock_path, 74 | dependencies = dependencies 75 | ) 76 | ``` 77 | 78 | ### Test the lambda docker image locally 79 | 80 | ```{r, eval = FALSE} 81 | payload <- list(number = 2) 82 | tag <- "parity1" 83 | test_lambda(tag = "parity1", payload) 84 | ``` 85 | 86 | ### Deploy to AWS Lambda 87 | 88 | ```{r, eval = FALSE} 89 | # Might take a while, its pushing it to a remote repository 90 | deploy_lambda(tag = "parity1") 91 | ``` 92 | 93 | ### Invoke deployed lambda 94 | 95 | ```{r, eval = FALSE} 96 | invoke_lambda( 97 | function_name = "parity1", 98 | invocation_type = "RequestResponse", 99 | payload = list(number = 2), 100 | include_logs = FALSE 101 | ) 102 | 103 | #> Lambda response payload: 104 | #> {"parity":"even"} 105 | ``` 106 | 107 | ## Code of Conduct 108 | 109 | Please note that the r2lambda project is released with a 110 | [Contributor Code of Conduct](https://contributor-covenant.org/version/2/1/CODE_OF_CONDUCT.html). By 111 | contributing to this project, you agree to abide by its terms. 112 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # r2lambda 5 | 6 | 7 | 8 | [![R-CMD-check](https://github.com/discindo/r2lambda/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/discindo/r2lambda/actions/workflows/R-CMD-check.yaml) 9 | [![Codecov test 10 | coverage](https://codecov.io/gh/discindo/r2lambda/branch/main/graph/badge.svg)](https://app.codecov.io/gh/discindo/r2lambda?branch=main) 11 | 12 | 13 | The goal of `{r2lambda}` is to make it easier to go from an `R` script 14 | to a deployed `AWS Lambda` function. 15 | 16 | ## Requirements 17 | 18 | - [docker](https://docs.docker.com/get-docker/) is required to build, 19 | tag, and push the image. 20 | 21 | ## Installation 22 | 23 | You can install the development version of `{r2lambda}` like so: 24 | 25 | ``` r 26 | # install_packages("remotes") 27 | remotes::install_github("discindo/r2lambda") 28 | ``` 29 | 30 | ## Setup 31 | 32 | `r2lambda` assumes credentials for connecting to AWS services are 33 | available in the `R` session. This can be done via an `.Renviron` file 34 | that should set enironmental variables like so: 35 | 36 | AWS_ACCESS_KEY_ID = "YOUR AWS ACCESS KEY ID" 37 | AWS_SECRET_ACCESS_KEY = "YOUR AWS SECRET ACCESS KEY" 38 | AWS_PROFILE = "YOUR AWS PROFILE" 39 | AWS_REGION = "YOUR AWS REGION" 40 | 41 | But since `r2lambda` uses `paws` under the hood, all authentication 42 | methods supported by `paws` are available in `r2lambda`. See 43 | [here](https://github.com/paws-r/paws/blob/main/docs/credentials.md) for 44 | details on setting credentials, region, profile, etc. 45 | 46 | ## Workflow 47 | 48 | ### Build a docker image for the lambda function 49 | 50 | ``` r 51 | runtime_function <- "parity" 52 | runtime_path <- system.file("parity.R", package = "r2lambda") 53 | renvlock_path <- system.file("renv.lock", package = "r2lambda") 54 | dependencies <- NULL 55 | 56 | # Might take a while, its building a docker image 57 | build_lambda( 58 | tag = "parity1", 59 | runtime_function = runtime_function, 60 | runtime_path = runtime_path, 61 | renvlock_path = renvlock_path, 62 | dependencies = dependencies 63 | ) 64 | ``` 65 | 66 | ### Test the lambda docker image locally 67 | 68 | ``` r 69 | payload <- list(number = 2) 70 | tag <- "parity1" 71 | test_lambda(tag = "parity1", payload) 72 | ``` 73 | 74 | ### Deploy to AWS Lambda 75 | 76 | ``` r 77 | # Might take a while, its pushing it to a remote repository 78 | deploy_lambda(tag = "parity1") 79 | ``` 80 | 81 | ### Invoke deployed lambda 82 | 83 | ``` r 84 | invoke_lambda( 85 | function_name = "parity1", 86 | invocation_type = "RequestResponse", 87 | payload = list(number = 2), 88 | include_logs = FALSE 89 | ) 90 | 91 | #> Lambda response payload: 92 | #> {"parity":"even"} 93 | ``` 94 | 95 | ## Code of Conduct 96 | 97 | Please note that the r2lambda project is released with a [Contributor 98 | Code of 99 | Conduct](https://contributor-covenant.org/version/2/1/CODE_OF_CONDUCT.html). 100 | By contributing to this project, you agree to abide by its terms. 101 | -------------------------------------------------------------------------------- /_pkgdown.yml: -------------------------------------------------------------------------------- 1 | url: https://discindo.github.io/r2lambda/ 2 | template: 3 | bootstrap: 5 4 | 5 | -------------------------------------------------------------------------------- /codecov.yml: -------------------------------------------------------------------------------- 1 | comment: false 2 | 3 | coverage: 4 | status: 5 | project: 6 | default: 7 | target: auto 8 | threshold: 1% 9 | informational: true 10 | patch: 11 | default: 12 | target: auto 13 | threshold: 1% 14 | informational: true 15 | -------------------------------------------------------------------------------- /inst/lambda-S3-put-role.json: -------------------------------------------------------------------------------- 1 | { 2 | "Version": "2012-10-17", 3 | "Statement": [ 4 | { 5 | "Sid": "S3put", 6 | "Action": [ 7 | "s3:putObject" 8 | ], 9 | "Effect": "Allow", 10 | "Resource": [ 11 | //Here goes the arn 12 | ] 13 | } 14 | ] 15 | } 16 | -------------------------------------------------------------------------------- /inst/lambda-role.json: -------------------------------------------------------------------------------- 1 | { 2 | "Version": "2012-10-17", 3 | "Statement": [ 4 | { 5 | "Effect": "Allow", 6 | "Principal": { 7 | "Service": [ 8 | "lambda.amazonaws.com" 9 | ] 10 | }, 11 | "Action": "sts:AssumeRole" 12 | } 13 | ] 14 | } 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /inst/lambdr_dockerfile.txt: -------------------------------------------------------------------------------- 1 | FROM public.ecr.aws/lambda/provided 2 | 3 | ENV R_VERSION=4.2.2 4 | 5 | RUN yum -y install wget git tar 6 | 7 | RUN yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm \ 8 | && wget https://cdn.rstudio.com/r/centos-7/pkgs/R-${R_VERSION}-1-1.x86_64.rpm \ 9 | && yum -y install R-${R_VERSION}-1-1.x86_64.rpm \ 10 | && rm R-${R_VERSION}-1-1.x86_64.rpm 11 | 12 | ENV PATH="${PATH}:/opt/R/${R_VERSION}/bin/" 13 | 14 | # System requirements for R packages 15 | RUN yum -y install openssl-devel 16 | 17 | RUN R -e 'install.packages(c("remotes", "lambdr", "renv"), repos = "https://packagemanager.rstudio.com/all/__linux__/centos7/latest")' 18 | 19 | RUN printf '#!/bin/sh\ncd /lambda\nRscript runtime.R' > /var/runtime/bootstrap \ 20 | && chmod +x /var/runtime/bootstrap 21 | 22 | WORKDIR /lambda 23 | COPY runtime.R runtime.R 24 | RUN chmod 755 -R /lambda 25 | -------------------------------------------------------------------------------- /inst/parity.R: -------------------------------------------------------------------------------- 1 | parity <- function(number) { 2 | list(parity = if (as.integer(number) %% 2 == 0) "even" else "odd") 3 | } 4 | 5 | lambdr::start_lambda() 6 | -------------------------------------------------------------------------------- /inst/renv.lock: -------------------------------------------------------------------------------- 1 | { 2 | "R": { 3 | "Version": "4.4.1", 4 | "Repositories": [ 5 | { 6 | "Name": "CRAN", 7 | "URL": "https://cloud.r-project.org" 8 | } 9 | ] 10 | }, 11 | "Packages": { 12 | "R6": { 13 | "Package": "R6", 14 | "Version": "2.5.1", 15 | "Source": "Repository", 16 | "Repository": "CRAN", 17 | "Requirements": [ 18 | "R" 19 | ], 20 | "Hash": "470851b6d5d0ac559e9d01bb352b4021" 21 | }, 22 | "askpass": { 23 | "Package": "askpass", 24 | "Version": "1.2.1", 25 | "Source": "Repository", 26 | "Repository": "CRAN", 27 | "Requirements": [ 28 | "sys" 29 | ], 30 | "Hash": "c39f4155b3ceb1a9a2799d700fbd4b6a" 31 | }, 32 | "curl": { 33 | "Package": "curl", 34 | "Version": "5.2.3", 35 | "Source": "Repository", 36 | "Repository": "CRAN", 37 | "Requirements": [ 38 | "R" 39 | ], 40 | "Hash": "d91263322a58af798f6cf3b13fd56dde" 41 | }, 42 | "httr": { 43 | "Package": "httr", 44 | "Version": "1.4.7", 45 | "Source": "Repository", 46 | "Repository": "CRAN", 47 | "Requirements": [ 48 | "R", 49 | "R6", 50 | "curl", 51 | "jsonlite", 52 | "mime", 53 | "openssl" 54 | ], 55 | "Hash": "ac107251d9d9fd72f0ca8049988f1d7f" 56 | }, 57 | "jsonlite": { 58 | "Package": "jsonlite", 59 | "Version": "1.8.9", 60 | "Source": "Repository", 61 | "Repository": "CRAN", 62 | "Requirements": [ 63 | "methods" 64 | ], 65 | "Hash": "4e993b65c2c3ffbffce7bb3e2c6f832b" 66 | }, 67 | "lambdr": { 68 | "Package": "lambdr", 69 | "Version": "1.2.5", 70 | "Source": "Repository", 71 | "Repository": "CRAN", 72 | "Requirements": [ 73 | "httr", 74 | "jsonlite", 75 | "logger" 76 | ], 77 | "Hash": "c5054d8272a53671dd101a17a34419a7" 78 | }, 79 | "logger": { 80 | "Package": "logger", 81 | "Version": "0.3.0", 82 | "Source": "Repository", 83 | "Repository": "CRAN", 84 | "Requirements": [ 85 | "utils" 86 | ], 87 | "Hash": "c145edf05cc128e6ffcfa5d872c46329" 88 | }, 89 | "mime": { 90 | "Package": "mime", 91 | "Version": "0.12", 92 | "Source": "Repository", 93 | "Repository": "CRAN", 94 | "Requirements": [ 95 | "tools" 96 | ], 97 | "Hash": "18e9c28c1d3ca1560ce30658b22ce104" 98 | }, 99 | "openssl": { 100 | "Package": "openssl", 101 | "Version": "2.2.2", 102 | "Source": "Repository", 103 | "Repository": "CRAN", 104 | "Requirements": [ 105 | "askpass" 106 | ], 107 | "Hash": "d413e0fef796c9401a4419485f709ca1" 108 | }, 109 | "renv": { 110 | "Package": "renv", 111 | "Version": "1.0.7", 112 | "Source": "Repository", 113 | "Repository": "CRAN", 114 | "Requirements": [ 115 | "utils" 116 | ], 117 | "Hash": "397b7b2a265bc5a7a06852524dabae20" 118 | }, 119 | "sys": { 120 | "Package": "sys", 121 | "Version": "3.4.3", 122 | "Source": "Repository", 123 | "Repository": "CRAN", 124 | "Hash": "de342ebfebdbf40477d0758d05426646" 125 | } 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /man/aws_connect.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/lambda-util.R 3 | \name{aws_connect} 4 | \alias{aws_connect} 5 | \title{connect to an aws service} 6 | \usage{ 7 | aws_connect(service) 8 | } 9 | \arguments{ 10 | \item{service}{character, the name of a service, e.g., "lambda" or "iam". 11 | Should be a function exported by `paws` (see `getNamespaceExports("paws")`)} 12 | } 13 | \description{ 14 | connect to an aws service 15 | } 16 | \examples{ 17 | \dontrun{ 18 | aws_connect("lambda") 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /man/build_lambda.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/lambda-deploy.R 3 | \name{build_lambda} 4 | \alias{build_lambda} 5 | \title{build and tag lambda image locally} 6 | \usage{ 7 | build_lambda( 8 | tag, 9 | runtime_function, 10 | runtime_path, 11 | support_path = NULL, 12 | renvlock_path = NULL, 13 | dependencies = NULL 14 | ) 15 | } 16 | \arguments{ 17 | \item{tag}{A name for the Docker container and Lambda function} 18 | 19 | \item{runtime_function}{name of the runtime function} 20 | 21 | \item{runtime_path}{path to the script containing the runtime function} 22 | 23 | \item{support_path}{path to the support files (if any). Either NULL 24 | (the default) if all needed code is in the same `runtime_path` script, or a 25 | character vector of paths to additional files needed by the runtime script.} 26 | 27 | \item{renvlock_path}{path to the renv.lock file (if any). Default is NULL.} 28 | 29 | \item{dependencies}{list of dependencies (if any). Default is NULL.} 30 | } 31 | \description{ 32 | build and tag lambda image locally 33 | } 34 | \details{ 35 | Use either `renvlock_path` or `dependencies` to install required 36 | packages, not both. By default, both are `NULL`, so the Docker image will 37 | have no additional packages installed. 38 | } 39 | -------------------------------------------------------------------------------- /man/deploy_lambda.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/lambda-deploy.R 3 | \name{deploy_lambda} 4 | \alias{deploy_lambda} 5 | \title{deploy a local lambda image to AWS Lambda} 6 | \usage{ 7 | deploy_lambda(tag, set_aws_envvars = FALSE, ...) 8 | } 9 | \arguments{ 10 | \item{tag}{The tag of an existing local image tagged with ECR repo 11 | (see `build_lambda`)} 12 | 13 | \item{set_aws_envvars}{logical, whether to set the local AWS secrets to the 14 | deployed Lambda environment (default = `FALSE`). This is useful if the 15 | Lambda needs to access other AWS service. When `TRUE`, the following 16 | envvars are set: `PROFILE`, `REGION`, `SECRET_ACCESS_KEY`, and 17 | `ACCESS_KEY_ID`. They are fetched using `Sys.getenv()`.} 18 | 19 | \item{...}{Arguments passed onto `create_lambda_function`} 20 | } 21 | \description{ 22 | deploy a local lambda image to AWS Lambda 23 | } 24 | \examples{ 25 | \dontrun{ 26 | 27 | runtime_function <- "parity" 28 | runtime_path <- system.file("parity.R", package = "r2lambda") 29 | dependencies <- NULL 30 | 31 | build_lambda( 32 | tag = "myrepo52", 33 | runtime_function = runtime_function, 34 | runtime_path = runtime_path, 35 | dependencies = dependencies 36 | ) 37 | 38 | deploy_lambda(tag = "myrepo52") 39 | 40 | invoke_lambda( 41 | function_name = "myrepo52", 42 | payload = list(number = 3), 43 | invocation_type = "RequestResponse" 44 | ) 45 | 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /man/invoke_lambda.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/lambda-deploy.R 3 | \name{invoke_lambda} 4 | \alias{invoke_lambda} 5 | \title{invoke a lambda function} 6 | \usage{ 7 | invoke_lambda(function_name, invocation_type, payload, include_logs = FALSE) 8 | } 9 | \arguments{ 10 | \item{function_name}{The name or arn of the function} 11 | 12 | \item{invocation_type}{One of ‘DryRun’, ‘RequestResponse’, 13 | or ‘Event’ see `?paws.compute::lambda_invoke`} 14 | 15 | \item{payload}{A named list internally converted to json} 16 | 17 | \item{include_logs}{logical, whether to show the lambda logs (default: FALSE)} 18 | } 19 | \description{ 20 | invoke a lambda function 21 | } 22 | \examples{ 23 | \dontrun{ 24 | invoke_lambda( 25 | function_name = "parity", 26 | payload = list(number = 3), 27 | invocation_type = "RequestResponse" 28 | ) 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /man/schedule_lambda.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/lambda-deploy.R 3 | \name{schedule_lambda} 4 | \alias{schedule_lambda} 5 | \title{Put a deployed lambda function on a schedule} 6 | \usage{ 7 | schedule_lambda(lambda_function, execution_rate) 8 | } 9 | \arguments{ 10 | \item{lambda_function}{character, the name (or tag) of the function. A check 11 | is done internally make sure the Lambda exists and fetch its ARN.} 12 | 13 | \item{execution_rate}{character, the rate to run the lambda function. Can use 14 | `rate` or `cron` specification. For details see the official documentation: 15 | https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-create-rule-schedule.html} 16 | } 17 | \description{ 18 | Put a deployed lambda function on a schedule 19 | } 20 | \examples{ 21 | \dontrun{ 22 | # Make Tidytuesday lambda fetch the Tidytuesday dataset every wednesday 23 | at 8 am schedule_lambda("Tidytuesday3", "cron(0 8 * * Wed)") 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /man/test_lambda.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/lambda-deploy.R 3 | \name{test_lambda} 4 | \alias{test_lambda} 5 | \title{test a lambda locally} 6 | \usage{ 7 | test_lambda(tag, payload) 8 | } 9 | \arguments{ 10 | \item{tag}{The tag of an existing local image tagged with ECR repo 11 | (see `build_lambda`)} 12 | 13 | \item{payload}{Named list. Arguments to lambda function.} 14 | } 15 | \description{ 16 | test a lambda locally 17 | } 18 | \examples{ 19 | \dontrun{ 20 | payload <- list(number = 2) 21 | tag <- "449283523352.dkr.ecr.us-east-1.amazonaws.com/myrepo51:latest" 22 | test_lambda(tag, payload) 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /r2lambda.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | 18 | BuildType: Package 19 | PackageUseDevtools: Yes 20 | PackageInstallArgs: --no-multiarch --with-keep.source 21 | PackageRoxygenize: rd,collate,namespace 22 | -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | # This file is part of the standard setup for testthat. 2 | # It is recommended that you do not modify it. 3 | # 4 | # Where should you do additional test configuration? 5 | # Learn more about the roles of various files in: 6 | # * https://r-pkgs.org/tests.html 7 | # * https://testthat.r-lib.org/reference/test_package.html#special-files 8 | 9 | library(testthat) 10 | library(r2lambda) 11 | 12 | test_check("r2lambda") 13 | -------------------------------------------------------------------------------- /tests/testthat/test-lambda-util.R: -------------------------------------------------------------------------------- 1 | if (FALSE) { # only in in interactive session 2 | 3 | test_that("aws_connect fails ok when bad service is requested", { 4 | expect_error(aws_connect("lambd")) 5 | }) 6 | 7 | test_that("aws_connect works", { 8 | checkmate::expect_list(aws_connect("lambda")) 9 | }) 10 | } 11 | 12 | test_that("install_deps_line fails ok with incorrect input", { 13 | deps <- list("a") 14 | expect_error(install_deps_line(deps = deps)) 15 | 16 | deps <- list(1) 17 | expect_error(install_deps_line(deps = deps)) 18 | 19 | deps <- c(1) 20 | expect_error(install_deps_line(deps = deps)) 21 | }) 22 | 23 | test_that("runtime_line fails ok with incorrect input", { 24 | runtime <- list("a") 25 | expect_error(runtime_line(runtime = runtime)) 26 | 27 | runtime <- list(1) 28 | expect_error(runtime_line(runtime = runtime)) 29 | 30 | runtime <- c(1) 31 | expect_error(runtime_line(runtime = runtime)) 32 | }) 33 | 34 | test_that("parse_password works", { 35 | expect_error(parse_password("AWS:my_password")) 36 | 37 | pass <- jsonlite::base64_enc("AWS:my_password") 38 | test <- parse_password(pass) 39 | expect_equal(test, "my_password") 40 | 41 | pass <- jsonlite::base64_enc("AWS:my_password:AWS:") 42 | test <- parse_password(pass) 43 | expect_equal(test, "my_password:AWS:") 44 | 45 | pass <- jsonlite::base64_enc("my_password") 46 | test <- parse_password(pass) 47 | expect_equal(test, "my_password") 48 | }) 49 | 50 | test_that("runtime_line works with correct input", { 51 | runtime <- "my_fun" 52 | test <- runtime_line(runtime = runtime) 53 | expect_equal(test, 'CMD ["my_fun"]') 54 | }) 55 | 56 | test_that("renv_line works", { 57 | expect_error(renv_line(renvlock = 1)) 58 | 59 | renvlock <- renv_line("renv.lock") 60 | test <- glue::glue_collapse( 61 | c( 62 | "COPY renv.lock renv.lock", 63 | "RUN Rscript -e 'renv::restore()'" 64 | ), 65 | sep = "\n" 66 | ) 67 | expect_equal(test, renvlock) 68 | }) 69 | 70 | ##### 71 | 72 | test_that("create_lambda_dockerfile works with correct input", { 73 | folder <- file.path(tempdir(), "test1") 74 | unlink(folder, recursive = TRUE) 75 | 76 | runtime_function <- "parity" 77 | runtime_path <- system.file("parity.R", package = "r2lambda") 78 | dependencies <- NULL 79 | 80 | create_lambda_dockerfile( 81 | folder = folder, 82 | runtime_function = runtime_function, 83 | runtime_path = runtime_path, 84 | dependencies = dependencies 85 | ) 86 | 87 | expect_true(dir.exists(folder)) 88 | expect_equal(dir(folder), c("Dockerfile", "runtime.R")) 89 | unlink(folder, recursive = TRUE) 90 | }) 91 | 92 | test_that("create_lambda_dockerfile fails as expected", { 93 | folder <- file.path(tempdir(), "test2") 94 | unlink(folder, recursive = TRUE) 95 | 96 | runtime_function <- "party" 97 | runtime_path <- system.file("party.R", package = "r2lambda") 98 | dependencies <- NULL 99 | 100 | expect_error( 101 | create_lambda_dockerfile( 102 | folder = folder, 103 | runtime_function = runtime_function, 104 | runtime_path = runtime_path, 105 | dependencies = dependencies 106 | ) 107 | ) 108 | unlink(folder, recursive = TRUE) 109 | 110 | runtime_function <- list("party") 111 | runtime_path <- system.file("party.R", package = "r2lambda") 112 | dependencies <- NULL 113 | 114 | expect_error( 115 | create_lambda_dockerfile( 116 | folder = folder, 117 | runtime_function = runtime_function, 118 | runtime_path = runtime_path, 119 | dependencies = dependencies 120 | ) 121 | ) 122 | unlink(folder, recursive = TRUE) 123 | 124 | runtime_function <- NA 125 | runtime_path <- system.file("parity.R", package = "r2lambda") 126 | dependencies <- NULL 127 | 128 | expect_error( 129 | create_lambda_dockerfile( 130 | folder = folder, 131 | runtime_function = runtime_function, 132 | runtime_path = runtime_path, 133 | dependencies = dependencies 134 | ) 135 | ) 136 | unlink(folder, recursive = TRUE) 137 | 138 | runtime_function <- NA 139 | runtime_path <- list(system.file("parity.R", package = "r2lambda")) 140 | dependencies <- NULL 141 | 142 | expect_error( 143 | create_lambda_dockerfile( 144 | folder = folder, 145 | runtime_function = runtime_function, 146 | runtime_path = runtime_path, 147 | dependencies = dependencies 148 | ) 149 | ) 150 | unlink(folder, recursive = TRUE) 151 | 152 | runtime_function <- "parity" 153 | runtime_path <- system.file("parity.R", package = "r2lambda") 154 | dependencies <- list() 155 | 156 | expect_error( 157 | create_lambda_dockerfile( 158 | folder = folder, 159 | runtime_function = runtime_function, 160 | runtime_path = runtime_path, 161 | dependencies = dependencies 162 | ) 163 | ) 164 | unlink(folder, recursive = TRUE) 165 | }) 166 | 167 | 168 | ##### 169 | 170 | test_that("create_lambda_image fails ok when inputs are incorrect", { 171 | folder <- file.path(tempdir(), paste0("test", runif(1))) 172 | unlink(folder, recursive = TRUE, force = TRUE) 173 | 174 | tag <- "testtag" 175 | folder <- file.path(tempdir(), paste0("test", runif(1))) 176 | expect_error(create_lambda_image(folder = folder, tag = tag)) 177 | 178 | tag <- "testtag" 179 | folder <- file.path(tempdir(), paste0("test", runif(1))) 180 | dir.create(folder) 181 | expect_error(create_lambda_image(folder = folder, tag = tag)) 182 | unlink(folder, recursive = TRUE) 183 | 184 | tag <- "testtag" 185 | folder <- file.path(tempdir(), paste0("test", runif(1))) 186 | dir.create(folder) 187 | file.create(file.path(folder, "Dockerfile")) 188 | expect_error(create_lambda_image(folder = folder, tag = tag)) 189 | unlink(folder, recursive = TRUE) 190 | 191 | tag <- "testtag" 192 | folder <- file.path(tempdir(), paste0("test", runif(1))) 193 | dir.create(folder) 194 | file.create(file.path(folder, "runtime.R")) 195 | expect_error(create_lambda_image(folder = folder, tag = tag)) 196 | unlink(folder, recursive = TRUE) 197 | 198 | tag <- "testtag" 199 | folder <- file.path(tempdir(), paste0("test", runif(1))) 200 | dir.create(folder) 201 | file.create(file.path(folder, "Dockerfile")) 202 | file.create(file.path(folder, "runtime.R")) 203 | expect_error(create_lambda_image(folder = folder, tag = tag)) 204 | unlink(folder, recursive = TRUE) 205 | 206 | tag <- "testtag" 207 | folder <- file.path(tempdir(), paste0("test", runif(1))) 208 | dir.create(folder) 209 | file.create(file.path(folder, "Dockerfile")) 210 | write( 211 | x = "test that file is not empty", 212 | file.path(folder, "Dockerfile"), append = TRUE 213 | ) 214 | file.create(file.path(folder, "runtime.R")) 215 | expect_error(create_lambda_image(folder = folder, tag = tag)) 216 | unlink(folder, recursive = TRUE) 217 | 218 | tag <- "testtag" 219 | folder <- file.path(tempdir(), paste0("test", runif(1))) 220 | dir.create(folder) 221 | file.create(file.path(folder, "Dockerfile")) 222 | file.create(file.path(folder, "runtime.R")) 223 | write( 224 | x = "test that file is not empty", 225 | file.path(folder, "runtime.R"), append = TRUE 226 | ) 227 | expect_error(create_lambda_image(folder = folder, tag = tag)) 228 | unlink(folder, recursive = TRUE) 229 | }) 230 | -------------------------------------------------------------------------------- /vignettes/.gitignore: -------------------------------------------------------------------------------- 1 | *.html 2 | *.R 3 | -------------------------------------------------------------------------------- /vignettes/lambda-s3.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "AWS Lambda and S3 integration" 3 | output: rmarkdown::html_vignette 4 | vignette: > 5 | %\VignetteIndexEntry{lambda-s3} 6 | %\VignetteEngine{knitr::rmarkdown} 7 | %\VignetteEncoding{UTF-8} 8 | --- 9 | 10 | ```{r, include = FALSE} 11 | knitr::opts_chunk$set( 12 | collapse = TRUE, 13 | comment = "#>", 14 | eval = FALSE 15 | ) 16 | ``` 17 | 18 | ```{r setup} 19 | library(r2lambda) 20 | library(tidytuesdayR) 21 | ``` 22 | 23 | # Overview 24 | 25 | At the end of this tutorial, we would have created an AWS Lambda function that fetches 26 | the most-recent Tidytuesday dataset and writes it into an S3 Bucket every Wednesday. 27 | To do this, we'll first work interactively with `{r2lambda}` and `{paws}` to go 28 | through all the steps the Lambda function would eventually need to do, then wrap 29 | the code and deploy it to AWS Lambda, and finally schedule it to run weekly. 30 | 31 | # Getting started with AWS Simple Storage Service (S3) from R 32 | 33 | As with any AWS service supported by `{paws}`, we can easily connect to S3 and 34 | perform some basic operations. Below, we establish an S3 service using `r2lambda::aws_connect`, 35 | then create a bucket called `tidytuesday-dataset`, drop and then delete and empty file, and 36 | delete the bucket altogether. This exercise is not very meaningful beyond learning 37 | the basics on how to interact with S3 from `R`. Eventually, though, our lambda function 38 | would need to do something similar, so being familiar with the process in an interactive 39 | session helps. 40 | 41 | **To run any of the code below, you need some environmental variables set. See 42 | the [Setup](https://github.com/discindo/r2lambda#build-a-docker-image-for-the-lambda-function) 43 | section in the `{r2lambda}` package readme for more details** 44 | 45 | ```{r} 46 | s3_service <- aws_connect("s3") 47 | 48 | # create a bucket on S3 49 | s3_service$create_bucket(Bucket = "a-unique-bucket") 50 | 51 | # upload an object to our bucket 52 | tmpfile <- tempfile(pattern = "object_", fileext = "txt") 53 | write("test", tmpfile) 54 | (readLines(tmpfile)) 55 | s3_service$put_object(Body = tmpfile, Bucket = "a-unique-bucket", Key = "TestFile") 56 | 57 | # list the contents of a bucket 58 | s3_service$list_objects(Bucket = "a-unique-bucket") 59 | 60 | # delete an object from a bucket 61 | s3_service$delete_object(Bucket = "a-unique-bucket", Key = "TestFile") 62 | 63 | # delete a bucket 64 | s3_service$delete_bucket(Bucket = "a-unique-bucket") 65 | ``` 66 | 67 | Now, the above procedure used a local file, but what if we generated some data during 68 | our session, and we want to stream that directly to S3 without saving to file? In 69 | many cases, we don't have the option to write to disk or simply don't want to. 70 | 71 | In such cases we need to serialize our data object before trying to `put` it in the bucket. 72 | This comes down to calling `serialize` with `connection=NULL` to generate a `raw` 73 | vector without writing to a file. We can then put the `iris` data set from memory into our 74 | `a-unique-bucket` S3 bucket. 75 | 76 | ```{r} 77 | s3_service <- aws_connect("s3") 78 | 79 | # create a bucket on S3 80 | s3_service$create_bucket(Bucket = "a-unique-bucket") 81 | 82 | # upload an object to our bucket 83 | siris <- serialize(iris, connection = NULL) 84 | s3_service$put_object(Body = siris, Bucket = "a-unique-bucket", Key = "TestFile2") 85 | 86 | # list the contents of a bucket 87 | s3_service$list_objects(Bucket = "a-unique-bucket") 88 | 89 | # delete an object from a bucket 90 | s3_service$delete_object(Bucket = "a-unique-bucket", Key = "TestFile2") 91 | 92 | # delete a bucket 93 | s3_service$delete_bucket(Bucket = "a-unique-bucket") 94 | ``` 95 | 96 | OK. With that, we now know the two steps our Lambda function would need to do: 97 | 98 | 1. fetch the most recent Tidytuesday data set (see 99 | [this post](https://discindo.org/post/an-r-aws-lambda-function-to-download-tidytuesday-datasets/) for details) 100 | 2. put the data set as an object in the S3 bucket 101 | 102 | Still in an interactive session, lets just write the code that our Lambda would have 103 | to execute. 104 | 105 | ```{r} 106 | library(tidytuesdayR) 107 | 108 | # Find the most recent tuesday and fetch the corresponding data set 109 | most_recent_tuesday <- tidytuesdayR::last_tuesday(date = Sys.Date()) 110 | tt_data <- tidytuesdayR::tt_load(x = most_recent_tuesday) 111 | 112 | # by default it comes as class `tt_data`, which causes problems 113 | # with serialization and conversion to JSON. So best to extract 114 | # the data set(s) as a simple list 115 | tt_data <- lapply(names(tt_data), function(x) tt_data[[x]]) 116 | 117 | # then serialize 118 | tt_data_raw <- serialize(tt_data, connection = NULL) 119 | 120 | # create a bucket on S3 121 | s3_service <- r2lambda::aws_connect("s3") 122 | s3_service$create_bucket(Bucket = "tidytuesday-datasets") 123 | 124 | # upload an object to our bucket 125 | s3_service$put_object( 126 | Body = tt_data_raw, 127 | Bucket = "tidytuesday-datasets", 128 | Key = most_recent_tuesday 129 | ) 130 | 131 | # list the contents of our bucket and find the Keys for all objects 132 | objects <- s3_service$list_objects(Bucket = "tidytuesday-datasets") 133 | sapply(objects$Contents, "[[", "Key") 134 | #> [1] "2023-03-07" 135 | 136 | # fetch a Tidytuesday dataset from S3 137 | tt_dataset <- s3_service$get_object( 138 | Bucket = "tidytuesday-datasets", 139 | Key = most_recent_tuesday 140 | ) 141 | 142 | # convert from raw and show the first few rows 143 | tt_dataset$Body |> unserialize() |> head() 144 | ``` 145 | 146 | Now we should have everything we need to write our Lambda function. 147 | 148 | # Lambda + S3 integration: Dropping a file in an S3 bucket 149 | 150 | Wrapping the above interactive code into a function and also, defining an `s3_connect` 151 | function as a helper to create an S3 client within the function. By doing this, we 152 | avoid adding `r2lambda` as a dependency to the Lambda function. (At the time of writing, 153 | `r2lambda` does not yet support non-CRAN packages.) 154 | 155 | ```{r} 156 | tidytuesday_lambda_s3 <- function() { 157 | most_recent_tuesday <- tidytuesdayR::last_tuesday(date = Sys.Date()) 158 | tt_data <- tidytuesdayR::tt_load(x = most_recent_tuesday) 159 | tt_data <- lapply(names(tt_data), function(x) tt_data[[x]]) 160 | tt_data_raw <- serialize(tt_data, connection = NULL) 161 | 162 | s3_service <- paws::s3() 163 | s3_service$put_object(Body = tt_data_raw, 164 | Bucket = "tidytuesday-datasets", 165 | Key = most_recent_tuesday) 166 | } 167 | ``` 168 | 169 | Now, calling `tidytuesday_lambda_s3()` should fetch and put the most recent 170 | Tidytuesday data set into our S3 bucket. To test it, we run: 171 | 172 | ```{r} 173 | tidytuesday_lambda_s3() 174 | 175 | list_objects <- function(bucket) { 176 | s3 <- s3_connect() 177 | obj <- s3$list_objects(Bucket = bucket) 178 | sapply(obj$Contents, "[[", "Key") 179 | } 180 | 181 | list_objects("tidytuesday-datasets") 182 | #> [1] "2023-03-07" 183 | ``` 184 | 185 | On to the next step, to create and deploy the Lambda function. We have a few 186 | considerations here: 187 | 188 | 1. We have some dependencies that would need to be available in the docker image. 189 | We already saw how to install `{tidytuesdayR}` in our Lambda docker image in a 190 | [previous post](https://discindo.org/post/an-r-aws-lambda-function-to-download-tidytuesday-datasets/). 191 | Besides this, we also need to install `{paws}`, because without it we can't interact 192 | with S3. To do this, we just need to add `dependencies = c("tidytuesdayR", "paws")` 193 | when building the image with `r2lambda::build_lambda`. 194 | 195 | 2. 196 | 197 | 1. For the Lambda function to connect to S3, it needs access to some environmental 198 | variables. The same ones as we have in our current interactive session without which 199 | we can't establish local clients of AWS services. These are: `REGION`, `PROFILE`, 200 | `SECRET_ACCESS_KEY`, and `ACCESS_KEY_ID`. To include these envvars in the Lambda 201 | docker image on deploy, use the `set_aws_envvars` argument of `deploy_lambda`. 202 | 203 | ## Build 204 | 205 | ```{r} 206 | r_code <- " 207 | s3_connect <- function() { 208 | paws::s3(config = list( 209 | credentials = list( 210 | creds = list( 211 | access_key_id = Sys.getenv('ACCESS_KEY_ID'), 212 | secret_access_key = Sys.getenv('SECRET_ACCESS_KEY') 213 | ), 214 | profile = Sys.getenv('PROFILE') 215 | ), 216 | region = Sys.getenv('REGION') 217 | )) 218 | } 219 | 220 | tidytuesday_lambda_s3 <- function() { 221 | most_recent_tuesday <- tidytuesdayR::last_tuesday(date = Sys.Date()) 222 | tt_data <- tidytuesdayR::tt_load(x = most_recent_tuesday) 223 | tt_data <- lapply(names(tt_data), function(x) tt_data[[x]]) 224 | tt_data_raw <- serialize(tt_data, connection = NULL) 225 | 226 | s3_service <- s3_connect() 227 | s3_service$put_object(Body = tt_data_raw, 228 | Bucket = 'tidytuesday-datasets', 229 | Key = most_recent_tuesday) 230 | } 231 | 232 | lambdr::start_lambda() 233 | " 234 | 235 | tmpfile <- tempfile(pattern = "tt_lambda_s3_", fileext = ".R") 236 | write(x = r_code, file = tmpfile) 237 | ``` 238 | 239 | ```{r} 240 | runtime_function <- "tidytuesday_lambda_s3" 241 | runtime_path <- tmpfile 242 | dependencies <- c("tidytuesdayR", "paws") 243 | 244 | r2lambda::build_lambda( 245 | tag = "tidytuesday_lambda_s3", 246 | runtime_function = runtime_function, 247 | runtime_path = runtime_path, 248 | dependencies = dependencies 249 | ) 250 | ``` 251 | 252 | ## Deploy 253 | 254 | We set a generous 2 minute timeout, just to be safe that the data set is successfully 255 | copied to S3. And we also increase the available memory to 1024 mb. Note also the 256 | flag to pass along our local AWS envvars to the deployed lambda environment. 257 | 258 | ```{r} 259 | r2lambda::deploy_lambda( 260 | tag = "tidytuesday_lambda_s3", 261 | set_aws_envvars = TRUE, 262 | Timeout = 120, 263 | MemorySize = 1024) 264 | ``` 265 | 266 | ## Invoke 267 | 268 | We invoke as usual, with an empty list as payload because our function does not 269 | take any arguments. 270 | 271 | ```{r} 272 | r2lambda::invoke_lambda( 273 | function_name = "tidytuesday_lambda_s3", 274 | invocation_type = "RequestResponse", 275 | payload = list(), 276 | include_logs = TRUE) 277 | 278 | #> INFO [2023-03-08 23:50:46] [invoke_lambda] Validating inputs. 279 | #> INFO [2023-03-08 23:50:46] [invoke_lambda] Checking function state. 280 | #> INFO [2023-03-08 23:50:47] [invoke_lambda] Function state: Active. 281 | #> INFO [2023-03-08 23:50:47] [invoke_lambda] Invoking function. 282 | #> 283 | #> Lambda response payload: 284 | #> {"Expiration":[],"ETag":"\"4f5a6085215b9074faed28d816696a99\"","ChecksumCRC32":[], 285 | #> "ChecksumCRC32C":[],"ChecksumSHA1":[],"ChecksumSHA256":[],"ServerSideEncryption":"AES256", 286 | #> "VersionId":[],"SSECustomerAlgorithm":[],"SSECustomerKeyMD5":[],"SSEKMSKeyId":[], 287 | #> "SSEKMSEncryptionContext":[],"BucketKeyEnabled":[],"RequestCharged":[]} 288 | #> 289 | #> Lambda logs: 290 | #> OpenBLAS WARNING - could not determine the L2 cache size on this system, assuming 256k 291 | #> INFO [2023-03-09 05:50:49] Using handler function tidytuesday_lambda_s3 292 | #> START RequestId: c6cb0600-3400-4ca3-9232-8af53542f8e8 Version: $LATEST 293 | #> --- Compiling #TidyTuesday Information for 2023-03-07 ---- 294 | #> --- There is 1 file available --- 295 | #> --- Starting Download --- 296 | #> Downloading file 1 of 1: `numbats.csv` 297 | #> --- Download complete --- 298 | #> END RequestId: c6cb0600-3400-4ca3-9232-8af53542f8e8 299 | #> REPORT RequestId: c6cb0600-3400-4ca3-9232-8af53542f8e8 Duration: 12061.06 ms 300 | #> Billed Duration: 13331 ms Memory Size: 1024 MB Max Memory Used: 181 MB Init 301 | #> Duration: 1269.59 ms 302 | #> SUCCESS [2023-03-08 23:51:01] [invoke_lambda] Done. 303 | ``` 304 | 305 | Then, to confirm that a Tidytuesday data set was written to S3 as an object in the 306 | bucket `tidytuesday-datasets` we would run: 307 | 308 | ```{r} 309 | s3_service <- r2lambda::aws_connect(service = "s3") 310 | objs <- s3_service$list_objects(Bucket = "tidytuesday-datasets") 311 | objs$Contents[[1]]$Key 312 | #> [1] "2023-03-07" 313 | ``` 314 | 315 | We expect to see one object with a `Key` matching the date of the most recent Tuesday. 316 | At the time of writing that is March 7, 2023. 317 | 318 | ## Schedule 319 | 320 | Finally, to copy the Tidytuesday dataset on a weekly basis, for example, every Wednesday, 321 | we would use `r2lambda::schedule_lambda` with an execution rate set by `cron`. 322 | 323 | First, to validate that things are working, we can set the lambda on a 5-minute 324 | schedule and check the time stamp on the on the S3 object to make sure it is updated 325 | every 5 minutes: 326 | 327 | ```{r} 328 | # schedule the lambda to execute every 5 minutes 329 | r2lambda::schedule_lambda( 330 | lambda_function = "tidytuesday_lambda_s3", 331 | execution_rate = "rate(5 minutes)" 332 | ) 333 | 334 | # occasionally query the S3 bucket status and the LastModified time stamp 335 | objs <- s3_service$list_objects(Bucket = "tidytuesday-datasets") 336 | objs$Contents[[1]]$LastModified 337 | ``` 338 | 339 | If all is well, set it to run every Wednesday at midnight: 340 | 341 | ```{r} 342 | r2lambda::schedule_lambda( 343 | lambda_function = "tidytuesday_lambda_s3", 344 | execution_rate = "cron(0 0 * * Wed *)" 345 | ) 346 | ``` 347 | 348 | Next Wednesday morning, we should have two objects, with keys matching the two 349 | most-recent Tuesdays. 350 | 351 | # Lambda + S3 integration: Triggering Lambda when on new file in S3 bucket 352 | ## TODO 353 | -------------------------------------------------------------------------------- /vignettes/schedule-lambda.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Set an AWS lambda function to run on a schedule" 3 | output: rmarkdown::html_vignette 4 | vignette: > 5 | %\VignetteIndexEntry{schedule-lambda} 6 | %\VignetteEngine{knitr::rmarkdown} 7 | %\VignetteEncoding{UTF-8} 8 | --- 9 | 10 | ```{r, include = FALSE} 11 | knitr::opts_chunk$set( 12 | collapse = TRUE, 13 | comment = "#>", 14 | eval = FALSE 15 | ) 16 | ``` 17 | 18 | ```{r setup} 19 | library(r2lambda) 20 | ``` 21 | 22 | In this exercise, we'll write a simple lambda runtime function, build a docker image locally, 23 | test the lambda invocation, deploy it to AWS Lambda, update it to run on a schedule, 24 | and check the AWS logs to confirm it executes at the correct times. 25 | 26 | ## A lambda runtime function 27 | 28 | We start with a simple function that does not require any input and does not return 29 | anything. If this example lambda is to run on a schedule, we don't want to worry 30 | about any input arguments. Also, we want this lambda function to simply have a 31 | side-effect, like printing something to the logs, without returning any data or writing 32 | to a database. This will help us greatly with the setup, in that we'll be able deploy 33 | and schedule the lambda with mininal involvement from other AWS services. 34 | 35 | With this in mind, we have the following function that simply prints the system time. 36 | Printing the current time makes sense because we can easily check that the lambda runs 37 | on the correct schedule from the logs. 38 | 39 | ```{r} 40 | current_time <- function() { 41 | print(paste("CURRENT TIME: ", Sys.time())) 42 | } 43 | ``` 44 | 45 | ## Build, test, and deploy 46 | 47 | Here, we follow the procedure described in `Tidy Tuesday dataset Lambda` vignette. 48 | We write this to a file that we'll use to build the lambda `docker` image: 49 | 50 | ```{r} 51 | r_code <- " 52 | current_time <- function() { 53 | print(paste('CURRENT TIME:', Sys.time())) 54 | } 55 | 56 | lambdr::start_lambda() 57 | " 58 | 59 | tmpfile <- tempfile(pattern = "current_time_lambda_", fileext = ".R") 60 | write(x = r_code, file = tmpfile) 61 | ``` 62 | 63 | And then build the `docker` image. Note that we don't have any dependencies other 64 | than base `R`. 65 | 66 | ```{r} 67 | r2lambda::build_lambda( 68 | tag = "current_time", 69 | runtime_function = "current_time", 70 | runtime_path = tmpfile, 71 | dependencies = NULL 72 | ) 73 | ``` 74 | 75 | We test the lambda docker container locally, because it makes sense. The console 76 | output should include the log messages and the standard output string showing the 77 | current time. 78 | 79 | ```{r} 80 | r2lambda::test_lambda(tag = "current_time", payload = list()) 81 | ``` 82 | 83 | Then, we deploy the lambda to AWS, leaving the lambda environment to its defaults, 84 | as 3 seconds should be enough to get and print the current time. 85 | 86 | ```{r} 87 | r2lambda::deploy_lambda(tag = "current_time") 88 | ``` 89 | 90 | Finally, we invoke the cloud instance of our function, to make sure everything went 91 | well. Be sure to include the logs, as this particular function does not return anything. 92 | 93 | ```{r} 94 | r2lambda::invoke_lambda( 95 | function_name = "current_time", 96 | invocation_type = "RequestResponse", 97 | payload = list(), 98 | include_logs = TRUE 99 | ) 100 | ``` 101 | 102 | ## Schedule to run every minute 103 | 104 | To make a lambda function run on a recurring schedule, we need to update an already 105 | deployed function. This involves three steps and two AWS services, Lambda and EventBridge: 106 | 107 | - creating a schedule event role (EventBridge, `paws::eventbridge`) 108 | - adding permissions to this role to invoke lambda functions (Lambda, `paws::lambda`) 109 | - adding our target lambda function to eventBridge 110 | 111 | Detailed instructions are available in the [AWS documentation](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-run-lambda-schedule.html). 112 | The function `schedule_lambda` abstracts these three steps in one go. To set a lambda 113 | on a schedule, we need the name of the function we wish to update, and the rate at which 114 | we want EventBridge to invoke it. Two rate-setting expression formats are supported, 115 | `cron` and `rate`. For example, to schedule a lambda to run every Sunday at midnight, 116 | we could use `execution_rate = "cron(0 0 * * Sun)"`. Alternatively, to schedule a lambda 117 | to run every 15 minutes, we might use `execution_rate = "rate(15 minutes)"`. The details are 118 | in this [article](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-create-rule-schedule.html) 119 | 120 | ```{r} 121 | r2lambda::schedule_lambda(lambda_function = "current_time", execution_rate = "rate(1 minute)") 122 | ``` 123 | 124 | ## Checking the AWS logs 125 | 126 | To see if our function runs every minute, we can take a look at the AWS logs. If the 127 | function was writing to a database, or dropping files in an S3 bucket, we could also check 128 | the contents of those resources for the effects of the scheduled lambda function. But as 129 | our example function only prints the current time, the only way to know that it indeed runs 130 | every minute is to check the logs. 131 | 132 | To do this, we'll use `paws` and `r2lambda::aws_connect` to establish an AWS CloudWatchLogs 133 | service locally, and fetch the recent logs to look for traces of our lambda function. 134 | 135 | 136 | In the first step, we connect to `cloudwatchlogs` and fetch the names of the log groups. 137 | Inspect the `logs` object below to find the name corresponding to the lambda function 138 | whose logs we want to fetch. 139 | 140 | ```{r} 141 | logs_service <- r2lambda::aws_connect(service = "cloudwatchlogs") 142 | logs <- logs_service$describe_log_groups() 143 | (logGroups <- sapply(logs$logGroups, "[[", 1)) 144 | ``` 145 | 146 | The, we can grab only the data for our scheduled lambda function: 147 | 148 | ```{r} 149 | current_time_lambda_logs <- logs_service$filter_log_events( 150 | logGroupName = "/aws/lambda/current_time") 151 | ``` 152 | 153 | And pull only the message printed by our `R` function wrapped in the lambda: 154 | 155 | ```{r} 156 | messages <- sapply(current_time_lambda_logs$events, "[[", "message") 157 | current_time_messages <- messages[grepl("CURRENT TIME", messages)] 158 | data.frame(Current_time_lambda = current_time_messages) 159 | 160 | #> Current_time_lambda 161 | #> 1 [1] "CURRENT TIME: 2023-02-26 22:53:55"\n 162 | #> 2 [1] "CURRENT TIME: 2023-02-26 22:54:41"\n 163 | #> 3 [1] "CURRENT TIME: 2023-02-26 22:55:41"\n 164 | #> 4 [1] "CURRENT TIME: 2023-02-26 22:56:41"\n 165 | #> 5 [1] "CURRENT TIME: 2023-02-26 22:57:41"\n 166 | 167 | ``` 168 | 169 | ## Clean up 170 | 171 | We don't want to let a this trivial lambda fire every minute, it will still incur 172 | some cost. So its wise to delete the event schedule rule and maybe even the lambda 173 | function it self. 174 | 175 | To remove the event rule, we first need to remove associated targets, and then remove 176 | the rule. 177 | 178 | ```{r} 179 | # connect to the EventBridge service 180 | events_service <- r2lambda::aws_connect("eventbridge") 181 | # find the names of all rules we need 182 | schedule_rules <- events_service$list_rules()[[1]] |> sapply("[[", 1) 183 | 184 | # find the targets associated with the rule we want to remove 185 | rule_to_remove <- schedule_rules[[1]] 186 | 187 | target_arn_to_remove <- events_service$list_targets_by_rule(Rule = rule_to_remove)$Targets[[1]]$Id 188 | events_service$remove_targets(Rule = rule_to_remove, Ids = target_arn_to_remove) 189 | events_service$delete_rule(Name = rule_to_remove) 190 | 191 | events_service$list_rules()[[1]] |> sapply("[[", 1) 192 | 193 | ``` 194 | 195 | Finally, to remove the Lambda: 196 | 197 | ```{r} 198 | lambda_service <- r2lambda::aws_connect("lambda") 199 | lambda_service$list_functions()$Functions |> sapply("[[","FunctionName") 200 | lambda_service$delete_function(FunctionName = "parity1") 201 | ``` 202 | 203 | -------------------------------------------------------------------------------- /vignettes/tt-lambda.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Tidy Tuesday dataset Lambda" 3 | output: rmarkdown::html_vignette 4 | vignette: > 5 | %\VignetteIndexEntry{tt-lambda} 6 | %\VignetteEngine{knitr::rmarkdown} 7 | %\VignetteEncoding{UTF-8} 8 | --- 9 | 10 | ```{r, include = FALSE} 11 | knitr::opts_chunk$set( 12 | collapse = TRUE, 13 | comment = "#>", 14 | eval = FALSE 15 | ) 16 | ``` 17 | 18 | ```{r setup} 19 | library(r2lambda) 20 | library(jsonlite) 21 | library(magrittr) 22 | ``` 23 | 24 | ## Use `{r2lambda}` to download Tidytuesday dataset 25 | 26 | In this exercize, we'll create an AWS Lambda function that downloads 27 | the [tidytuesday](https://github.com/rfordatascience/tidytuesday/tree/master/data/2023/2023-02-07) 28 | data set for the most recent Tuesday (or most recent Tuesday from a date of interest). 29 | 30 | ## Runtime function 31 | 32 | The first step is to write the runtime function. This is the function that will be 33 | executed when we invoke the Lambda function after it has been deployed. To download 34 | the Tidytuesday data set, we will use the `{tidytuesdayR}` package. In the runtime 35 | script, we define a function called `tidytyesday_lambda` that takes one optional 36 | argument `date`. If `date` is omitted, the function returns the data set(s) for the most 37 | recent Tuesday, otherwise, it looks up the most recent Tuesday from a date of interest 38 | and returns the corresponding data set(s). 39 | 40 | ```{r} 41 | library(tidytuesdayR) 42 | 43 | tidytuesday_lambda <- function(date = NULL) { 44 | if (is.null(date)) 45 | date <- Sys.Date() 46 | 47 | most_recent_tuesday <- tidytuesdayR::last_tuesday(date = date) 48 | tt_data <- tidytuesdayR::tt_load(x = most_recent_tuesday) 49 | data_names <- names(tt_data) 50 | data_list <- lapply(data_names, function(x) tt_data[[x]]) 51 | return(data_list) 52 | } 53 | 54 | tidytuesday_lambda("2022-02-02") 55 | ``` 56 | 57 | ## R script to build the lambda 58 | 59 | To build the lambda image, we need an `R` script that sources any required code, 60 | loads any needed libraries, defines a runtime function, and ends with a call to 61 | `lambdr::start_lambda()`. The runtime function does not have to be defined in this 62 | file. We could, for example, source another script, or load a package and set a 63 | loaded function as the runtime function in the subsequent call to `r2lambda::build_lambda` 64 | (see below). We save this script to a file and record the path: 65 | 66 | ```{r} 67 | r_code <- " 68 | library(tidytuesdayR) 69 | 70 | tidytuesday_lambda <- function(date = NULL) { 71 | if (is.null(date)) 72 | date <- Sys.Date() 73 | 74 | most_recent_tuesday <- tidytuesdayR::last_tuesday(date = date) 75 | tt_data <- tidytuesdayR::tt_load(x = most_recent_tuesday) 76 | data_names <- names(tt_data) 77 | data_list <- lapply(data_names, function(x) tt_data[[x]]) 78 | return(data_list) 79 | } 80 | 81 | lambdr::start_lambda() 82 | " 83 | 84 | tmpfile <- tempfile(pattern = "ttlambda_", fileext = ".R") 85 | write(x = r_code, file = tmpfile) 86 | ``` 87 | 88 | ## Build, test, and deploy the lambda function 89 | 90 | ### 1. Build 91 | 92 | - We set the `runtime_function` argument to the name of the function we wish the 93 | `docker` container to run when invoked. In this case, this is `tidytuesday_lambda`. 94 | This adds a `CMD` instruction to the `Dockerfile` 95 | 96 | - We set the `runtime_path` argument to the path we stored the script defining our 97 | runtime function. 98 | 99 | - We set the `dependencies` argument to `c("tidytuesdayR")`because we need to 100 | have the `tidytuesdayR` package installed within the `docker` container if we are 101 | to download the dataset. This steps adds a `RUN` instruction to the `Dockerfile` 102 | that calls `install.packages` to install `{tidytuesdayR}` from CRAN. 103 | 104 | - Finally, the `tag` argument sets the name of our Lambda function which we'll use 105 | later to test and invoke the function. The `tag` argument also becomes the name of 106 | the folder that `{r2lambda}` will create to build the image. This folder will have 107 | two files, `Dockerfile` and `runtime.R`. `runtime.R` is our script from `runtime_path`, 108 | renamed before it is copied in the `docker` image with a `COPY` instruction. 109 | 110 | ```{r} 111 | runtime_function <- "tidytuesday_lambda" 112 | runtime_path <- tmpfile 113 | dependencies <- "tidytuesdayR" 114 | 115 | r2lambda::build_lambda( 116 | tag = "tidytuesday3", 117 | runtime_function = runtime_function, 118 | runtime_path = runtime_path, 119 | dependencies = dependencies 120 | ) 121 | ``` 122 | 123 | ### 2. Test 124 | 125 | To make sure our Lambda `docker` container works as intended, we start it locally, 126 | and invoke it to test the response. The response is a list of three elements: 127 | 128 | ```{r} 129 | response <- r2lambda::test_lambda(tag = "tidytuesday3", payload = list(date = Sys.Date())) 130 | ``` 131 | 132 | - `status`, should be 0 if the test worked, 133 | - `stdout`, the standard output stream of the invocation, and 134 | - `stderr`, the standard error stream of the invocation 135 | 136 | `stdout` and `stderr` are `raw` vectors that we need to parse, for example: 137 | 138 | ```{r} 139 | rawToChar(response$stdout) 140 | ``` 141 | 142 | If the `stdout` slot of the response returns the correct output of our function, 143 | we are good to deploy to AWS. 144 | 145 | ### 3. Deploy 146 | 147 | The deploy step is simple, in that all we need to do is specify the name (tag) of 148 | the Lambda function we wish to push to AWS ECR. The `deploy_lambda` function also 149 | accepts `...`, which are named arguments ultimately passed onto 150 | `paws.compute:::lambda_create_function`. This is the function that calls the Lambda 151 | API. To see all available arguments run `?paws.compute:::lambda_create_function`. 152 | 153 | The most important arguments are probably `Timeout` and `MemorySize`, which set 154 | the time our function will be allowed to run and the amount of memory it will have 155 | available. In many cases it will make sense to increase the defaults of 3 seconds 156 | and 128 mb. 157 | 158 | ```{r} 159 | r2lambda::deploy_lambda(tag = "tidytuesday3", Timeout = 30) 160 | ``` 161 | 162 | ### 4. Invoke 163 | 164 | If all goes well, our function should now be available on the cloud awaiting requests. 165 | We can invoke it from `R` using `invoke_lambda`. The arguments are: 166 | 167 | - `function_name` -- the name of the function 168 | - `invocation_type` -- typically `RequestResponse` 169 | - `include_log` -- whether to print the logs of the run on the console 170 | - `payload` -- a named list with arguments sent to the `runtime_function`. In this 171 | case, the runtime function, `tidytuesday_lambda` has a single argument `date`, so 172 | the corresponding list is `list(date = Sys.Date())`. As our function can be called 173 | without any argument, we can also send and empty list as the payload. 174 | 175 | ```{r} 176 | response <- r2lambda::invoke_lambda( 177 | function_name = "tidytuesday3", 178 | invocation_type = "RequestResponse", 179 | payload = list(), 180 | include_logs = TRUE 181 | ) 182 | ``` 183 | 184 | Just like in the local test, the response payload comes as a raw vector that needs to 185 | be parsed into a data.frame: 186 | 187 | ```{r} 188 | tidytuesday_dataset <- response$Payload |> 189 | rawToChar() |> 190 | jsonlite::fromJSON(simplifyDataFrame = TRUE) 191 | 192 | tidytuesday_dataset[[1]][1:5, 1:5] 193 | ``` 194 | 195 | --------------------------------------------------------------------------------